Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在Python-socket中侦听UDP数据包。错误:[Errno 49]Can';无法分配请求的地址_Python_Sockets_Udp_Client Server - Fatal编程技术网

无法在Python-socket中侦听UDP数据包。错误:[Errno 49]Can';无法分配请求的地址

无法在Python-socket中侦听UDP数据包。错误:[Errno 49]Can';无法分配请求的地址,python,sockets,udp,client-server,Python,Sockets,Udp,Client Server,我正在尝试侦听通过本地网络发送的UDP数据包 我曾尝试过毫无乐趣地使用和许多其他教程 我正在用windows笔记本电脑发送此数据包 import socket import time Host = '192.168.1.7' Port = 5050 Message = 'hello world'+time.asctime() sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) while True: sock.sendto(

我正在尝试侦听通过本地网络发送的UDP数据包

我曾尝试过毫无乐趣地使用和许多其他教程

我正在用windows笔记本电脑发送此数据包

import socket
import time

Host = '192.168.1.7'
Port = 5050
Message = 'hello world'+time.asctime()

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

while True:
   sock.sendto(Message(Host,Port)
   print 'sent', Message
   time.sleep(5)
我使用了以下方法来尝试接收UDP数据包

import socket

Host = '192.168.1.138'
Port = 5050

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

sock.bind((Host, 5050))

while True:
    recieved = sock.recv(1024)
    print recieved

我在localhost上运行了以下Receive方法,效果非常好,但在局域网上分配设备的ip地址后,我会得到以下结果:

/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /Users/Myname/Desktop/untitled/recieve.py
Traceback (most recent call last):
  File "/Users/Myname/Desktop/untitled/recieve.py", line 12, in <module>
    sock.bind((UDP_IP, UDP_PORT))
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 49] Can't assign requested address

Process finished with exit code 1
/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7/Users/Myname/Desktop/untitled/recieve.py
回溯(最近一次呼叫最后一次):
文件“/Users/Myname/Desktop/untitled/recieve.py”,第12行,在
sock.bind((UDP_IP,UDP_端口))
文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py”,第228行,meth格式
返回getattr(self.\u sock,name)(*args)
socket.error:[Errno 49]无法分配请求的地址
进程已完成,退出代码为1

我在4个运行python 2.7的不同设备上进行了测试,这与我得到的信息相同。它是在windows 7笔记本电脑、MacBook、Kali Linux笔记本电脑和raspberryPi上的PIXEL Raspbian上运行的

也许您想要做的是sock.bind((无,UDP_端口))?通过这种方式,您将在所有本地网络接口上接收传入数据包,而不仅仅是在IP地址为192.168.1.138的网络接口上。类似错误回退(最后一次调用):文件“/Users/Faiz/Desktop/untitled/recieve.py”,第12行,在sock.bind((无,UDP_端口))文件中“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py”,第228行,在meth return getattr(self._sock,name)(*args)TypeError:compressing to Unicode:need string或buffer,NoneType foundOops,对不起,我的意思是:sock.bind(“”,UDP\u PORT))它可以工作!!!谢谢!也许你想做的是sock.bind((无,UDP_端口))?这样,您将在所有本地网络接口上接收传入数据包,而不仅仅是在IP地址为192.168.1.138的网络接口上。类似的错误回写(最后一次调用):文件“/Users/Faiz/Desktop/untitled/recieve.py”,第12行,在sock.bind((无,UDP_端口))文件中”/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py“,第228行,在meth return getattr(self.\u sock,name)(*args)TypeError:强制使用Unicode:需要字符串或缓冲区,非类型foundOops,对不起,我的意思是:sock.bind(“”,UDP\u PORT))它可以工作!!!谢谢!
/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /Users/Myname/Desktop/untitled/recieve.py
Traceback (most recent call last):
  File "/Users/Myname/Desktop/untitled/recieve.py", line 12, in <module>
    sock.bind((UDP_IP, UDP_PORT))
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 49] Can't assign requested address

Process finished with exit code 1