python网络编程之最简单的单工通信

tcp_server.py

from socket import *

server = socket(AF_INET, SOCK_STREAM)
server.bind(('',12345))
server.listen(5)
print 'wait for connect...'
while True:
client,address = server.accept()
print address,client
while True:
data = client.recv(1024)
print data
send_data = raw_input('>')
if not send_data:
break
client.send(send_data)
client.close()
server.close()

tcp_client.py

from socket import *

server = socket(AF_INET,SOCK_STREAM)
server.connect(('127.0.0.1',12345))
while True:
message = raw_input('client>')
if not message:
break
server.send(message)
data = server.recv(1024)
print 'recv is %s' % data
server.close()
上一篇:[Python 网络编程] TCP、简单socket模拟ssh (一)


下一篇:Python并发编程-线程-一个简单的例子