Python 技术篇-httpClient库连接服务器发送请求解析响应实例演示,No module named ‘httplib‘问题解决方法

时间:2022-07-25
本文章向大家介绍Python 技术篇-httpClient库连接服务器发送请求解析响应实例演示,No module named ‘httplib‘问题解决方法,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

新的 python 已经由 httplib 包变成了 http.client。 所以如果还引入 httplib 库就会报错:ModuleNotFoundError: No module named 'httplib'

import http.client

httpClient = http.client.HTTPConnection('10.10.xx.xx',5554)
# 发送请求,直接用参数/,相当于直接访问ip+端口号
httpClient.request('GET','/')
# 获取请求
response = httpClient.getresponse()
# 分解response回应消息
print("status:"+str(response.status))
# print(response.reason)
# print(response.read())
print('-'*5+'Headers'+'-'*5)
print(response.getheaders())
print('-'*5+'Message'+'-'*5)
print(response.msg)

执行代码后结果如下:

status:200
-----Headers-----
[('Accept-Ranges', 'bytes'), ('ETag', 'W/"93-1589209308000"'), ('Last-Modified',
 'Mon, 11 May 2020 15:01:48 GMT'), ('Content-Type', 'text/html'), ('Content-Leng
th', '93'), ('Date', 'Thu, 06 Aug 2020 13:23:28 GMT'), ('Server', 'server')]
-----Message-----
Accept-Ranges: bytes
ETag: W/"93-1589209308000"
Last-Modified: Mon, 11 May 2020 15:01:48 GMT
Content-Type: text/html
Content-Length: 93
Date: Thu, 06 Aug 2020 13:23:28 GMT
Server: server