转载:ImportError: No module named ‘httplib‘

转载文章:https://www.cnblogs.com/xiaodai0/p/10937919.html,主要为了记录一下

原因:Python 2.x中的"httplib"模块在Python 3.x中变成了"http.client"

python2的原代码:

import httplib
import urllib

reqheaders={
'MobileType':'Android',
'DeviceToken':'xxxxxxxxx',
'OSVersion':'1.0.3',
'AppVersion':'14',
'Host':'192.xxx.x.xxxx'}  

reqconn=httplib.HTTPConnection("192.xxx.x.xxxx")
reqconn.request("GET", "/Login?username=1416&password=123", None, reqheaders)
res=reqconn.getresponse()
print res.status,  res.reason
print res.msg
print res.read()

python3修改后代码:

import http.client    #修改引用的模块
import urllib

reqheaders={
'MobileType':'Android',
'DeviceToken':'xxxxxxxxx',
'OSVersion':'1.0.3',
'AppVersion':'14',
'Host':'192.xxx.x.xxxx'}  

reqconn=http.client.HTTPConnection("192.xxx.x.xxxx")  #修改对应的方法
reqconn.request("GET", "/Login?username=1416&password=123", None, reqheaders)
res=reqconn.getresponse()
print (res.status,  res.reason)
print (res.msg)
print (res.read())

 

你可能感兴趣的:(Python学习笔记,httplib,http.client)