简单的搭建一个服务器
server:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('localhost', 9401))
sock.listen(15)
connection,address = sock.acept()
while True:
try:
connection.settimeout(15)
buf = connection.recv(1024)
print(address)
print('address:', buf)
except socket.timeout:
print 'time out'
connection.close()
tcp client:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('192.168.1.169', 9401))
import time
while True:
time.sleep(2)
sock.send('ok')
print sock.recv(1024)
sock.close()