Main Page/wifiRobot

From Imperial College Robotics Society
Jump to: navigation, search


Random python UDP code

http://wiki.python.org/moin/UdpCommunication

python web hosting to look into: http://flask.pocoo.org/

Sender:

import socket
import sys
 
IP =  "127.0.0.1"
PORT = 5005
 
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
 
while 1:
        print "Enter message:"
        msg = raw_input()
        print "sending"
        sock.sendto(msg, (IP, PORT))
        print "sent"

Listener:

import socket
 
host = ''
port = 5005
 
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
 
s.bind((host, port))
 
while 1:
        msg, addr = s.recvfrom(8192)
        print msg, addr