新的 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