Piccola chat fatta per imparare ad usare i socket
Server:
Client:
Server:
Codice:
# Server Pychat
import socket
from time import sleep
print "---------------- Pychat 1.0 ----------------"
print "--------------- Created by R_ --------------"
print
host = raw_input("Host: ")
nickname = raw_input("Insert your nickname: ")
print
print "Nick:", nickname
print "Host:", host
print
sleep(2)
porta = 8080
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, porta))
s.listen(1)
conn, addr = s.accept()
print "A connection with", addr, "was estabilished"
sleep(2)
print "To close the connection print 'quit' on the message box"
sleep(1)
while True:
rec = conn.recv(1024)
print rec
mex = raw_input(">>>")
if mex == "quit":
break
mex_to_send = nickname +": " + mex
conn.send(mex_to_send)
s.close()
Client:
Codice:
# Client Pychat
import socket
from time import sleep
print "---------------- Pychat 1.0 ----------------"
print "--------------- Created by R_ --------------"
print
host = raw_input("Host: ")
nickname = raw_input("Insert your nickname: ")
print
print "Nick:", nickname
print "Host:", host
print
sleep(2)
porta = 8080
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, porta))
print "A connection with", host, "was estabilished"
sleep(2)
print "To close the connection print 'quit' on the message box"
sleep(1)
while True:
mex = raw_input(">>> ")
if mex == "quit":
break
mex_to_send = nickname + ": " + mex
s.send(mex_to_send)
rec = conn.recv(1024)
print rec
s.close()