python通讯编程_python----网络编程(TCP通讯)

python----网络编程(TCP通讯)

笔者在学习Python核心编程(第三版)这本书籍的时候

在章节网络编程 2.4.1----TCP服务端和客户端里面

尝试按照书籍编写TCP服务端和客户端的程序

书籍例子如下

TCP服务端

#coding=utf-8#创建TCP服务器

from socket import *

from time importctime

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

tcpSerSock=socket(AF_INET,SOCK_STREAM) #创服务器套接字

tcpSerSock.bind(ADDR) #套接字与地址绑定

tcpSerSock.listen(5) #监听连接,传入连接请求的最大数

whileTrue:print('waiting for connection...')

tcpCliSock,addr=tcpSerSock.accept()print('...connected from:',addr)whileTrue:

data=tcpCliSock.recv(BUFSIZ)#print('date=',data)

if notdata:breaktcpCliSock.send(('[%s] %s' %(ctime(),data)))

tcpCliSock.close()

tcpSerSock.close()

TCP客户端

#coding=utf-8

from socket import *HOST= 'localhost' #or 'localhost'

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

tcpCliSock=socket(AF_INET,SOCK_STREAM)

tcpCliSock.connect(ADDR)whileTrue:

data= input('>')print('data=',data);if notdata:breaktcpCliSock.send(data)

data=tcpCliSock.recv(BUFSIZ)if notdata:break

print(data)

tcpCliSock.close()

在启动服务端后,再启动客户端去连接服务器,输入信息,在服务器会返回报错:

TypeError: a bytes-like object is required, not ‘str’

笔者纠结了很久,在百度上搜索了相关的文章,看到了作者“MrYx”的一篇文章

https://blog.csdn.net/yexiaohhjk/article/details/68066843

里面有说道

In python 3, bytes strings and unicode strings are now two different types. Since sockets are not aware of string encodings, they are using raw bytes strings, that have a slightly different interface from unicode strings.

So, now, whenever you have a unicode string that you need to use as a byte string, you need to encode() it. And whenyou have a byte string, you need to decode it to use it as a regular(python 2.x) string.

Unicode strings are quotes enclosed strings. Bytes strings are b”” enclosed strings

When you use client_socket.send(data),replace it by client_socket.send(data.encode()). When you get datausing data = client_socket.recv(512), replace it by data =client_socket.recv(512).decode()

这里大致的意思就是,Pyhon3之后,bytes strings 和 unicode strings 是两种不同的字符类型。如果想要将unicode strings变成bytes strings,我们需要用encode()这种方式去对其进行转码。反之,我们则需要用到decode()。而在书籍的例子里面,在关于变量data,并没有考虑到这点。我们从socket里面接受或者发送的数据,应该是bytes类型的,但是我们在程序里面编辑的时候,需要的字符类型是unicode的,因此,我们需要对书籍的例子作出修改。

修改如下

TCP服务端

#!/usr/bin/python3#-*- coding -*-

from socket import *

from time importctime

HOST= ''PORT= 21571BUFSIZ= 1024ADDR=(HOST,PORT)

tcpSerSock=socket(AF_INET,SOCK_STREAM)

tcpSerSock.bind(ADDR)

tcpSerSock.listen(5)whileTrue:print ('waiting for connection')

tcpCliSock ,addr=tcpSerSock.accept()print ('...connected from:',addr)whileTrue:

data=tcpCliSock.recv(BUFSIZ).decode()if notdata:breaktcpCliSock.send(('[%s] %s' %(ctime(),data)).encode())

tcpCliSock.close()

tcpCliSock.close()

TCP客户端

#!/usr/bin/python3#-*- coding -*-

from socket import *HOST= '127.0.0.1'PORT= 21571BUFSIZ= 1024ADDR=(HOST,PORT)

tcpCliSock=socket(AF_INET, SOCK_STREAM)

tcpCliSock.connect(ADDR)whileTrue:

data= input('>')if notdata:breaktcpCliSock.send(data.encode())

data=tcpCliSock.recv(BUFSIZ).decode()if notdata:break

print(data)

tcpCliSock.close()

twisted:

服务端

#!/usr/bin/python3#-*- coding -*-

from twisted.internet importprotocol,reactorfrom time importctime

PORT= 21500

classTSServProtocol(protocol.Protocol):defconnectionMade(self):

clnt= self.clnt =self.transport.getPeer().hostprint('...connected from:',clnt)defdataReceived(self, data):

self.transport.write(('[%s] %s'%(ctime(),data)).encode())

factory=protocol.Factory()

factory.protocol=TSServProtocolprint('waiting for connection...')

reactor.listenTCP(PORT,factory)

reactor.run()

客户端

#!/usr/bin/python3#-*- coding -*-

from twisted.internet importprotocol,reactor

HOST= 'localhost'PORT= 21500

classTSClntProtocol(protocol.Protocol):defsendData(self):

data= input('>')ifdata:print('...sending %s...'%data)

self.transport.write(data.encode())else:

self.transport.loseConnection()defconnectionMade(self):

self.sendData()defdataReceived(self, data):

self.sendData()classTSClntFactory(protocol.ClientFactory):

protocol=TSClntProtocol

clientConnectionLost= clientConnentionFailed =\lambdaself,connector,reason:reactor.stop()

reactor.connectTCP(HOST,PORT,TSClntFactory())

reactor.run()

本文参考

https://blog.csdn.net/yexiaohhjk/article/details/68066843

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值