python - ConnectionRefusedError: [Errno 111] Connection refused
問(wèn)題描述
Python3實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的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)問(wèn),錯(cuò)在哪里?應(yīng)該該怎么改?
問(wèn)題解答
回答1:找到答案了,bing(’0.0.0.0’,port)即可。
相關(guān)文章:
1. node.js - nodejs+express+vue2. javascript - 我的站點(diǎn)貌似被別人克隆了, google 搜索特定文章,除了域名不一樣,其他的都一樣,如何解決?3. java - web端百度網(wǎng)盤的一個(gè)操作為什么要分兩次請(qǐng)求服務(wù)器, 有什么好處嗎4. 數(shù)據(jù)庫(kù) - Mysql的存儲(chǔ)過(guò)程真的是個(gè)坑!求助下面的存儲(chǔ)過(guò)程哪里錯(cuò)啦,實(shí)在是找不到哪里的問(wèn)題了。5. javascript - 如何獲取未來(lái)元素的父元素在頁(yè)面中所有相同元素中是第幾個(gè)?6. python - 如何把152753這個(gè)字符串轉(zhuǎn)變成時(shí)間格式15:27:537. 使用mysql命令行連接遠(yuǎn)程數(shù)據(jù)庫(kù)host跳轉(zhuǎn)8. javascript - onclick事件點(diǎn)擊不起作用9. javascript - webpack-dev-server和webpack沖突嗎10. python 字符串匹配問(wèn)題

網(wǎng)公網(wǎng)安備