创建TCP服务器
# 创建服务器套接字:
HOST = '127.0.0.1'
PORT = 21567
ADDR = (HOST, PORT)
BUSIZE = 1024
tcpsocket = socket.socket()
tcpsocket.bind(ADDR)
while True:
print("waiting for connect")
tcpsciket,addr = tcpsocket.accept() #tcpsciket 为连接
print("----connected from---- ", addr)
while True:
date =tcpsciket.recv(BUSIZE)
if not date:
break
t = '[%s] %s' % (ctime(), str(date))
print(t)
tcpsciket.send(bytes(t, encoding='utf-8'))
tcpsciket.close()
import socket
Host = '127.0.0.1'
PORT = 21567
BUFSIZE = 1024
ADDR = (Host, PORT)
tcpCliSock = socket.socket()
tcpCliSock.connect(ADDR)
while True:
data = input('> ')
if not data:
break
tcpCliSock.send(bytes(data, encoding = 'utf-8'))
print(data)
data = str(tcpCliSock.recv(BUFSIZE), encoding = 'utf-8')
if not data:
break
print(data)
tcpCliSock.close()