无法在Python-socket.error中侦听UDP数据包:[Errno 49]无法分配请求的地址

时间:2017-02-21 04:15:51

标签: python sockets udp client-server

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

我尝试使用https://wiki.python.org/moin/UdpCommunication没有任何乐趣和其他许多教程。

我从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

import socket


UDP_IP = "192.168.1.138"

UDP_PORT = 5050

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

sock.bind((UDP_IP, UDP_PORT))


while True:

    data, addr = sock.recvfrom(1024)  # buffer size is 1024 bytes

    print "received message:", data

我已经在localhost上运行了以下接收方法并且工作正常但是只要我在我的局域网上分配设备的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

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

0 个答案:

没有答案