python - ConnectionRefusedError: [Errno 111] Connection refused
問題描述
Python3實(shí)現(xiàn)了一個(gè)簡單的udp server和udp client。host指定為’localhost’時(shí),在同一臺(tái)機(jī)器上是運(yùn)行正常的。
udpserver.py:
from socket import *HOST = ’localhost’PORT = 9999s = socket(AF_INET,SOCK_DGRAM)s.bind((HOST,PORT))print(’...waiting for message..’)while True:data,address = s.recvfrom(1024)print(data,address)s.sendto(’this is the UDP server’.encode(’utf-8’), address)s.close()
udpclient.py:
from socket import *HOST=’localhost’#HOST=’deque.me’PORT=9999s = socket(AF_INET,SOCK_DGRAM)s.connect((HOST,PORT))while True:message = input(’send message: ’)s.sendall(message.encode(’utf-8’))data = s.recv(1024)print(data)s.close()
如果將udpclient.py里的host改為'deque.me',程序會(huì)出現(xiàn)錯(cuò)誤。
如果udpclient.py和udpserver.py運(yùn)行在同一臺(tái)機(jī)器上,也就是’deque.me’這臺(tái)服務(wù)器上,錯(cuò)誤如下:
ubuntu@VM-117-216-ubuntu:~/Shield/Py3$ python3 udpclient.py send message: testTraceback (most recent call last): File 'udpclient.py', line 12, in <module> data = s.recv(1024)ConnectionRefusedError: [Errno 111] Connection refused
如果把udpclietn.py放在另一臺(tái)windows機(jī)器上執(zhí)行,錯(cuò)誤提示圖下:
D:ShieldPy3>python udpclient.pysend message: testTraceback (most recent call last): File 'udpclient.py', line 11, in <module> data = s.recv(1024)ConnectionResetError: [WinError 10054] 遠(yuǎn)程主機(jī)強(qiáng)迫關(guān)閉了一個(gè)現(xiàn)有的連接。
試了試將udpserver.py中的host改為’deque.me’和’115.159.29.211’(公網(wǎng)IP地址),均出現(xiàn)如下錯(cuò)誤:
root@VM-117-216-ubuntu:~/Shield/Py3# python3 udpserver.pyTraceback (most recent call last): File 'udpserver.py', line 7, in <module> s.bind((HOST,PORT))OSError: [Errno 99] Cannot assign requested address
肯定的是’deque.me’是能正確解析到這Linux服務(wù)器的。請(qǐng)問,錯(cuò)在哪里?應(yīng)該該怎么改?
問題解答
回答1:找到答案了,bing(’0.0.0.0’,port)即可。
相關(guān)文章:
1. 安全性測試 - nodejs中如何防m(xù)ySQL注入2. javascript - 關(guān)于apply()與call()的問題3. html - eclipse 標(biāo)簽錯(cuò)誤4. python 利用subprocess庫調(diào)用mplayer時(shí)發(fā)生錯(cuò)誤5. python - Pycharm的Debug用不了6. datetime - Python如何獲取當(dāng)前時(shí)間7. 請(qǐng)問PHPstudy中的數(shù)據(jù)庫如何創(chuàng)建索引8. python - pycharm 自動(dòng)刪除行尾空格9. python文檔怎么查看?10. javascript - nginx反向代理靜態(tài)資源403錯(cuò)誤?
