Python udp connection: Unterschied zwischen den Versionen

Aus xinux.net
Zur Navigation springen Zur Suche springen
 
Zeile 1: Zeile 1:
 +
=Client=
 
<pre>
 
<pre>
 
#!/usr/bin/python
 
#!/usr/bin/python
Zeile 14: Zeile 15:
 
             socket.SOCK_DGRAM) # UDP
 
             socket.SOCK_DGRAM) # UDP
 
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
 
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
 +
</pre>
 +
=Server=
 +
<pre>
 +
#!/usr/bin/python
 +
import socket
 +
 +
UDP_IP_ADDRESS = "0.0.0.0"
 +
UDP_PORT_NO = 999
 +
serverSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
 +
serverSock.bind((UDP_IP_ADDRESS, UDP_PORT_NO))
 +
while True:
 +
    data, addr = serverSock.recvfrom(1024)
 +
    print "Message: ", data
 
</pre>
 
</pre>
 
=Links=
 
=Links=
 
*https://wiki.python.org/moin/UdpCommunication
 
*https://wiki.python.org/moin/UdpCommunication
 
*https://tutorialedge.net/python/udp-client-server-python/
 
*https://tutorialedge.net/python/udp-client-server-python/

Aktuelle Version vom 18. Dezember 2017, 18:00 Uhr

Client

#!/usr/bin/python
import socket

UDP_IP = "127.0.0.1"
UDP_PORT = 999
MESSAGE = "Hello, World!"

print "UDP target IP:", UDP_IP
print "UDP target port:", UDP_PORT
print "message:", MESSAGE

sock = socket.socket(socket.AF_INET, # Internet
             socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))

Server

#!/usr/bin/python
import socket

UDP_IP_ADDRESS = "0.0.0.0"
UDP_PORT_NO = 999
serverSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
serverSock.bind((UDP_IP_ADDRESS, UDP_PORT_NO))
while True:
    data, addr = serverSock.recvfrom(1024)
    print "Message: ", data

Links