zeroflyyer
2017-08-23 02:25
采纳率: 0%
浏览 1.0k

SocketServer TCP运行的时候报TypeError的错,这个怎么解决?

SocketServer TCP服务器

 #!usr/bin/python3

from socketserver import (TCPServer as TCP,StreamRequestHandler as SRH)
from time import ctime

HOST=''
PORT=21567
ADDR=(HOST,PORT)

class MyRequestHandler(SRH):
    def handle(self):
        print('...connected from:',self.client_address)
        self.wfile.write('[%s]%s'%(ctime(),self.rfile.readline()))


tcpServ=TCP(ADDR,MyRequestHandler)
print('waiting for connection ...')
tcpServ.serve_forever()
~                      

SocketServer TCP客户端

 #!usr/bin/python3
#-*-coding:UTF-8 -*-
from socket import *

HOST='localhost'
PORT=21567
BUFSIZ=1024
ADDR=(HOST,PORT)

while True:
    tcpCliSock =socket(AF_INET,SOCK_STREAM)
    tcpCliSock.connect(ADDR)
    data=input('> ')
    if not data:
        break
    tcpCliSock.send('%s\r\n'%data)
    data=tcpCliSock.recv(BUFSIZ)
    if not data:
        break
    print(data.strip())
    tcpCliSock.close()

运行客户端的时候报错如下:

hi
Traceback (most recent call last):
File "tsTclntSS.py", line 16, in
tcpCliSock.send('%s\r\n'%data)
TypeError: a bytes-like object is required, not 'str'

这个如何解决呢?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题