Python Sockets
Um simples Client/Server em Python:
Server: servMod.py
from socket import *
HOST = 'localhost'
PORT = '21567'
BUFSIZ = 1024
ADDR = (HOST, PORT)
tcpServSock = socket(AF_INET, SOCK_STREAM)
tcpServSock.bind(ADDR)
tcpServSock.listen(5)
while 1:
print "Esperando ligação"
tcpCliSock, addr = tcpServSock.accept()
print "ligado a: ", addr
while 1:
data = tcpCliSock.recv(BUFSIZ)
if not data: break
tcpCliSock.send("recebendo.... > " + data)
tcpCliSock.close()
tcpServSock.close()
Client: cliMod.py
from socket import *
HOST = 'localhost'
PORT = '21567'
BUFSIZ = 1024
ADDR = (HOST, PORT)
tcpCliSock = socket(AF_INET, SOCK_STREAM)
tcpCliSock.connect(ADDR)
while 1:
data = raw_input('>')
if not data: break
tcpCliSock.send(data)
data = tcpCliSock.recv(BUFSIZ)
if not data: break
print "ligado a ", ADDR, " - dados - ", data
tcpCliSock.close()
Post a Comment