python - ConnectionRefusedError: [Errno 111] Connection refused
問題描述
Python3實現了一個簡單的udp server和udp client。host指定為’localhost’時,在同一臺機器上是運行正常的。
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',程序會出現錯誤。
如果udpclient.py和udpserver.py運行在同一臺機器上,也就是’deque.me’這臺服務器上,錯誤如下:
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機器上執行,錯誤提示圖下:
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] 遠程主機強迫關閉了一個現有的連接。
試了試將udpserver.py中的host改為’deque.me’和’115.159.29.211’(公網IP地址),均出現如下錯誤:
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服務器的。請問,錯在哪里?應該該怎么改?
問題解答
回答1:找到答案了,bing(’0.0.0.0’,port)即可。
相關文章:
1. 前端 - node vue webpack項目文件結構2. 如何分別在Windows下用Winform項模板+C#,在MacOSX下用Cocos Application項目模板+Objective-C實現一個制作游戲的空的黑窗口?3. html5和Flash對抗是什么情況?4. php如何獲取訪問者路由器的mac地址5. javascript - 在 vue里面用import引入js文件,結果為undefined6. Java反射問題:為什么android.os.Message的recycleUnchecked方法不能通過反射獲取到?7. 小程序怎么加外鏈,語句怎么寫!求救新手,開文檔沒發現8. python - linux怎么在每天的凌晨2點執行一次這個log.py文件9. javascript - vue-resource中如何設置全局的timeout?10. thinkPHP5中獲取數據庫數據后默認選中下拉框的值,傳遞到后臺消失不見。有圖有代碼,希望有人幫忙
