Import the httplib module from a new version of the zero-learning python series to report the ImportError Solution

Source: Internet
Author: User
When you use the new version of python to open the old version of the Code, some errors or incompatibility may occur, today, we will analyze one of the cases. Before using httplib in Python 2.7 for interface testing, the running code is normal,

After using Python 3.3 recently, I checked the previous Code and found that the import httplib has an error: Unresolved import: httplib,

When running the code, the following error occurs: ImportError: No module named 'httplib'

Search for various materials and find the original Python 2. the "httplib" module in x is in Python 3. x is changed to "http. client ", it is strange that we only know the difference between the two versions, that is, the print statement is output in 3. add () in x. It seems that we need to know all the differences in detail ~

I have attached the code for your reference. I hope you will not get stuck in this error ~

Code implementation in 2.7:

The Code is as follows:


Import httplib
Import urllib

Reqheaders = {
'Leletype': 'android ',
'Devicetoken': 'xxxxxxxxxxx ',
'Osversion': '1. 0.3 ',
'Appversion': '14 ',
'Host': '2017. 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 ()

Implementation of code in version 3.3:

The Code is as follows:


Import http. client # modify the referenced Module
Import urllib

Reqheaders = {
'Leletype': 'android ',
'Devicetoken': 'xxxxxxxxxxx ',
'Osversion': '1. 0.3 ',
'Appversion': '14 ',
'Host': '2017. 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 ())

Attached references

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.