I am trying to create a simple Python client / server chat application with sockets and ultimately turn it into a Rock, Paper, Scissors network game.
I found a guide for creating a client / server, but I had problems with changing the loops, so that each script listens for another, receives a message, and then shows raw_input, which becomes a message sent to another script, then so on. Here is the code:
client.py
import socket
s = socket.socket()
host = socket.gethostname()
port = 12221
s.connect((host, port))
while True:
z = raw_input("Enter something for the server: ")
s.send(z)
print s.recv(1024)
server.py
import socket
s = socket.socket()
host = socket.gethostname()
port = 12221
s.bind((host, port))
s.listen(5)
while True:
c, addr = s.accept()
print 'Got connection from', addr
print c.recv(1024)
q = raw_input("Enter something to this client: ")
c.send(q)
Any help? Thanks.
source
share