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