[python]代码库
#!/usr/bin/env python
# TCP服务器demo
from socket import *
from time import ctime
HOST = ''
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST, PORT)
tcpSerSock = socket(AF_INET, SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(5)
while True:
print('waiting for connecting...')
tcpClisock, addr = tcpSerSock.accept()
print('...connected from:', addr)
while True:
data = tcpClisock.recv(BUFSIZ)
if not data:
break
tcpClisock.send('[%s] %s' % (bytes(ctime(), 'utf-8'), data))
tcpClisock.close()
tcpSerSock.close()