1 鱼币
python核心编程的一个例子
tsTclnt.py
---------------------------------
from socket import *
HOST = '127.0.0.1'
PORT = 21567
BUFSIZE = 1024
ADDR = (HOST, PORT)
tcpCliSock = socket(AF_INET, SOCK_STREAM)
tcpCliSock.connect(ADDR)
while True:
data = input('> ')
if not data:
break
tcpCliSock.send(data)
data = tcpCliSock.recv(BUFSIZE)
if not data:
break
print(data.decode('utf-8'))
tcpCliSock.close()
----------------------------
tsTserv3.py
-------------------------------------------------
from socket import *
from time import ctime
HOST = ''
PORT = 21567
BUFSIZE = 1024
ADDR = (HOST, PORT)
tcpSerSock = socket(AF_INET, SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(5)
while True:
print("稍等......")
tcpCliSock, addr = tcpSerSock.accept()
print("正在连接:", addr)
while True:
data = tcpCliSock.recv(BUFSIZE)
if not data:
break
tcpCliSock.send("[%s] %s" % (bytes(ctime(), 'utf-8'), data))
tcpCliSock.close()
tcpSerSock.close()
----------------------------------------------运行之后
> hi
Traceback (most recent call last):
File "tsTclnt.py", line 15, in <module>
tcpCliSock.send(data)
TypeError: a bytes-like object is required, not 'str'
我来回答