server端
#!/usr/bin/env python
import socket
import time
host=''
port=99999
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind((host,port))
s.listen(2)
while 1:
conn,addr = s.accept()
#---------------
print 'got connection from:',addr
while 1:
data = conn.recv(4096)
print 'get data',data
if not data: #判断是否接受到值
time.sleep(1,5)
continue
conn.sendall(data.upper())
print 'asdfasf',data
conn.close()
接受TCP连接并返回(conn,address),其中conn是新的套接字对象,可以用来接收和发送数据。address是连接客户端的地址。
client端
#!/usr/bin/env python
import socket
import time
h = '192.168.5.55'
p = 99999
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((h,p))
while 1:
s.send('hello word')
received_data = s.recv(1024)
time.sleep(1.5)
print 'safasfasf',received_data
s.close()