Import the httplib module from the new version of the zero learning python series to import the ImportError solution

  • 2020-04-02 13:42:27
  • OfStack

When we tested the interface with Python version 2.7 httplib, the code was running normally.

After starting Python 3.3 recently, I went back to the previous code and found an error in import httplib: Unresolved import: httplib,

An error is also reported when running the code: ImportError: No module named 'httplib'

I found that the "httplib" module in Python 2.x was changed into "http.client" in Python 3.x. I only knew the difference between the two versions before

Attached code for reference, hope you will not be this error pit ~

Code implementation in version 2.7:


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()

Code implementation in version 3.3:


import http.client    # Modify the referenced module 
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")  # Modify the corresponding method 
reqconn.request("GET", "/Login?username=1416&password=123", None, reqheaders)
res=reqconn.getresponse()
print (res.status,  res.reason)
print (res.msg)
print (res.read())

Attach (link: https://docs.python.org/3/library/http.client.html)


Related articles: