python----网络编程(TCP通讯),,python----
python----网络编程(TCP通讯),,python----
python----网络编程(TCP通讯)
笔者在学习Python核心编程(第三版)这本书籍的时候
在章节网络编程 2.4.1----TCP服务端和客户端里面
尝试按照书籍编写TCP服务端和客户端的程序
书籍例子如下
TCP服务端
#coding=utf-8#创建TCP服务器from socket import *from time import ctimeHOST=‘‘PORT=21567BUFSIZ=1024ADDR=(HOST,PORT)tcpSerSock=socket(AF_INET,SOCK_STREAM) #创服务器套接字tcpSerSock.bind(ADDR) #套接字与地址绑定tcpSerSock.listen(5) #监听连接,传入连接请求的最大数while True: print(‘waiting for connection...‘) tcpCliSock,addr =tcpSerSock.accept() print(‘...connected from:‘,addr) while True: data =tcpCliSock.recv(BUFSIZ) #print(‘date=‘,data) if not data: break tcpCliSock.send((‘[%s] %s‘ %(ctime(),data))) tcpCliSock.close()tcpSerSock.close()
TCP客户端
#coding=utf-8from socket import *HOST = ‘localhost‘ # or ‘localhost‘PORT = 21567BUFSIZ = 1024ADDR=(HOST,PORT)tcpCliSock = socket(AF_INET,SOCK_STREAM)tcpCliSock.connect(ADDR)while True: data = input(‘> ‘) print(‘data=‘,data); if not data: break tcpCliSock.send(data) data = tcpCliSock.recv(BUFSIZ) if not data: 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 import ctimeHOST = ‘‘PORT = 21571BUFSIZ = 1024ADDR = (HOST,PORT)tcpSerSock = socket(AF_INET,SOCK_STREAM)tcpSerSock.bind(ADDR)tcpSerSock.listen(5)while True: print (‘waiting for connection‘) tcpCliSock ,addr = tcpSerSock.accept() print (‘...connected from:‘,addr) while True: data = tcpCliSock.recv(BUFSIZ).decode() if not data: break tcpCliSock.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)while True: data = input(‘> ‘) if not data: break tcpCliSock.send(data.encode()) data = tcpCliSock.recv(BUFSIZ).decode() if not data: break print (data)tcpCliSock.close()
本文参考
https://blog.csdn.net/yexiaohhjk/article/details/68066843
python----网络编程(TCP通讯)
相关内容
- python3 内置函数,, 函数就是以功能为
- LeetCode 546. 移除盒子 | Python,,546. 移除盒子题
- python中zip的应用,,#!/usr/bin
- Python中的*args和**kwarg,,可变参数 *args
- python(七):元类与抽象基类,,一、实例创建 在创
- Python爬虫小记,,1.selenium
- python 入门 guess 函数,,猜年龄限制次数,使用
- Python3.4 安装 pycrypto 提示错误的处理办法,,今天做接口
- python100例 31-40,,031 判断星期题目
- Python2和Python3的区别(未完待续。。。。),,1、print函
评论关闭