py中网络编解码问题解法

py网络编码问题怎么办?

答:在encode()和decode()中加入参数,一般都是'utf-8',毕竟py默认的是'ascii'。

啥是encode()、decode()?

答:上网搜,一堆大佬,这里简单解释下,就是将字符串按照指定标准在bytes和str反复横跳,

bytes->str为解码;

str->bytes为编码;

因为方便网络传输,socket只支持传输bytes,所以bytes成为了“中转站”。

你的文章好短啊?!

答:路虽然短,但坑非常深,这短短的文章我却饶了长长的路。

You are to retrieve the following document using the HTTP protocol in a way that you can examine the HTTP Response headers. http://data.pr4e.org/intro-short.txt There are three ways that you might retrieve this web page and look at the response headers: Preferred: Modify the socket1.py program to retrieve the above URL and print out the headers and data. Make sure to change the code to retrieve the above URL - the values are different for each URL. Open the URL in a web browser with a developer console or FireBug and manually examine the headers that are returned. Use the telnet program as shown in lecture to retrieve the headers and content. Enter the header values in each of the fields below and press "Submit". Last-Modified: ETag: Content-Length: Cache-Control: Content-Type: socket1.py的内容:注意不能命名为socket,不然和lib重名了。 import socket mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) mysock.connect(('data.pr4e.org', 80)) cmd = 'GET http://data.pr4e.org/romeo.txt HTTP/1.0\r\n\r\n'.encode() mysock.send(cmd) while True: data = mysock.recv(512) if len(data) < 1: break print(data.decode(),end='') mysock.close() AI写代码 html 我的解法1(老师推荐的): import socket mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) mysock.connect(('data.pr4e.org',80)) cmd = 'GET http://data.pr4e.org/intro-short.txt HTTP/1.0\r\n\r\n'.encode() mysock.send(cmd) while True: data = mysock.recv(512) if(len(data) < 1): break print(data.decode()) mysock.close() 我的解法2: 进入网站:http://data.pr4e.org/intro-short.txt,点F12,点Network,Ctrl+R,点击intro-short.txt的Headers寻找答案。 可能存在的解法3: ———————————————— 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 原文链接:https://blog.youkuaiyun.com/zjt1027/article/details/104213562
09-07
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值