Summary of http request method libraries in Python
Recently I am using python for interface testing 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 own library urllib2 is used more often. A simple way to use it 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()
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 is too complicated? You have to read the document every time you write, let’s take a look at the third one
Third-party library--requests
Sending a get request is super simple:
print requests.get('http://localhost:8080).text
Just one sentence, let’s take a look at the post request
payload = {'key1': 'value1', 'key2': 'value2'} r = requests.post("http://httpbin.org/post", data=payload) print r.text
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
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 网址]',params) result = resultHtml.read() print result

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

Using python in Linux terminal...
