Summary of http request method libraries in Python

高洛峰
Release: 2017-03-16 17:07:57
Original
1097 people have browsed it

Recently using python to do interface testing, I found that there are many http request methods in python. I will take some time to sort out the relevant content today and share it with you. Everyone, the specific content is as follows:

1. python’s own library----urllib2

Python’s own library urllib2 is used more often, and the simple use 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. The simple use 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()
Copy after login

Simple get request

Let’s see if 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()
Copy after login


## is I think it's too complicated. You have to read the document every time you write, let’s take a look at the third one

3. Third-party library--requests

It’s super easy to send a get request:

print requests.get('http://localhost:8080).text
Copy after login

Just In a word, let’s look at the post request

payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.post("http://httpbin.org/post", data=payload)
print r.text
Copy after login

which is also very simple.

Let’s take a 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
Copy after login

Is it much simpler than urllib2, and requests comes with json parsing. This is great

http request in python

import urllib
params = urllib.urlencode({key:value,key:value})
resultHtml = urllib.urlopen('[API or 网址]',params)
result = resultHtml.read()
print result
Copy after login


The above is the detailed content of Summary of http request method libraries in Python. For more information, please follow other related articles on the PHP Chinese website!

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!