python - ConnectionRefusedError: [Errno 111] Connection refused
問題描述
Python3實(shí)現(xiàn)了一個簡單的udp server和udp client。host指定為’localhost’時,在同一臺機(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',程序會出現(xiàn)錯誤。
如果udpclient.py和udpserver.py運(yùn)行在同一臺機(jī)器上,也就是’deque.me’這臺服務(wù)器上,錯誤如下:
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放在另一臺windows機(jī)器上執(zhí)行,錯誤提示圖下:
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)閉了一個現(xiàn)有的連接。
試了試將udpserver.py中的host改為’deque.me’和’115.159.29.211’(公網(wǎng)IP地址),均出現(xiàn)如下錯誤:
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ù)器的。請問,錯在哪里?應(yīng)該該怎么改?
問題解答
回答1:找到答案了,bing(’0.0.0.0’,port)即可。
相關(guān)文章:
1. windows誤人子弟啊2. php傳對應(yīng)的id值為什么傳不了啊有木有大神會的看我下方截圖3. 如何用筆記本上的apache做微信開發(fā)的服務(wù)器4. python - linux 下用wsgifunc 運(yùn)行web.py該如何修改代碼5. 關(guān)于mysql聯(lián)合查詢一對多的顯示結(jié)果問題6. 實(shí)現(xiàn)bing搜索工具urlAPI提交7. 冒昧問一下,我這php代碼哪里出錯了???8. mysql優(yōu)化 - MySQL如何為配置表建立索引?9. MySQL主鍵沖突時的更新操作和替換操作在功能上有什么差別(如圖)10. 數(shù)據(jù)庫 - Mysql的存儲過程真的是個坑!求助下面的存儲過程哪里錯啦,實(shí)在是找不到哪里的問題了。
