Simple scripts to chat between computers in the same network.
Both computers must be running both of these scripts and target ip addresses must be set correctly.
(IP address of a computer can be found using ipconfig command.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | # Save as server.py
# Message Receiver
import os
from socket import *
host = ""
port = 13000
buf = 1024
addr = (host, port)
UDPSock = socket(AF_INET, SOCK_DGRAM)
UDPSock.bind(addr)
print "Waiting to receive messages..."
while True:
(data, addr) = UDPSock.recvfrom(buf)
print "Received message: " + data
if data == "exit":
break
UDPSock.close()
os._exit(0)
# Save as client.py
# Message Sender
import os
from socket import *
host = "127.0.0.1" # set to IP address of target computer
port = 13000
addr = (host, port)
UDPSock = socket(AF_INET, SOCK_DGRAM)
while True:
data = raw_input("Enter message to send or type 'exit': ")
UDPSock.sendto(data, addr)
if data == "exit":
break
UDPSock.close()
os._exit(0)
|