Summary of http request method libraries in Python

高洛峰
Release: 2016-11-23 13:28:47
Original
1899 people have browsed it

I am using python for interface testing recently and found that there are many http request methods in python. Today I will take some time to sort out the relevant content and share it with you. The specific content is as follows:
1. python’s own library----urllib2

Python’s built-in library urllib2 is commonly used. The simple usage is as follows:

import urllib2

response = urllib2.urlopen('http://localhost:8080/jenkins/api/json?pretty=true')

print response.read()

Simple get request

import urllib2

import urllib

post_data = urllib.urlencode({})

response = urllib2.urlopen('http://localhost:8080/, post_data )

print response.read()

print response.getheaders()

This is the simplest example of urllib2 sending a post. There are a lot of codes
2. Python's own library - httplib

httplib is a relatively low-level http request module, and urlib is encapsulated based on httplib. Simple usage is as follows:
import httplib
conn = httplib.HTTPConnection("www.python.org")
conn.request("GET", "/index.html")
r1 = conn.getresponse()
print r1. status, r1.reason
data1 = r1.read()
conn.request("GET", "/parrot.spam")
r2 = conn.getresponse()
data2 = r2.read()
conn.close( )

Simple get request

Let’s look at the post request
import httplib, urllib
params = urllib.urlencode({'@number': 12524, '@type': 'issue', '@action': 'show '})
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
conn = httplib.HTTPConnection("bugs.python.org")
conn.request("POST", "", params, headers)
response = conn.getresponse()
data = response.read()
print data
conn.close()


Do you think it’s too complicated? . You have to read the document every time you write, let’s take a look at the third one
Three-party library--requests

Sending a get request is super simple:
print requests.get('http://localhost:8080).text

Just one sentence, let’s look at the post request again
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.post("http://httpbin.org/post", data=payload)
print r.text

is also very simple.

Look again if you want to authenticate:
url = 'http://localhost:8080'
r = requests.post(url, data={}, auth=HTTPBasicAuth('admin', 'admin'))
print r.status_code
print r.headers
print r.reason

Isn’t it much simpler than urllib2, and requests come with json parsing. This is great

http request in python
import urllib
params = urllib.urlencode({key:value,key:value})
resultHtml = urllib.urlopen('[API or URL]',params)
result = resultHtml.read()
print result

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!