当前位置:首页 > 后端开发 > python中http请求方法库汇总

python中http请求方法库汇总

6个月前 (05-24)43

最近在使用python做接口测试,发现python中http请求方法有很多种,现汇总如下:

一、python自带库----urllib2

 python自带库urllib2使用的比较多,简单使用如下:

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

简单的get请求

import urllib2
import urllib
post_data = urllib.urlencode({})
response = urllib2.urlopen('http://localhost:8080/, post_data)
print response.read()
print response.getheaders()

这就是最简单的urllib2发送post例子。代码比较多

二、python自带库--httplib

httplib是一个相对底层的http请求模块,urlib就是基于httplib封装的。简单使用如下:

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()

简单的get请求

我们再来看post请求

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()

是不是觉得太复杂了。每次写还得再翻文档,看看第三种吧

三、第三方库--requests


发请get请求超级简单:

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

就一句话,再来看看post请求

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

也很简单。

再看看如果要认证:

url = 'http://localhost:8080'
r = requests.post(url, data={}, auth=HTTPBasicAuth('admin', 'admin'))
print r.status_code
print r.headers
print r.reason

是不是比urllib2更简单多了吧,且requests自带json解析。这点非常棒

 

具体更多内容,大家去看python文档,如下

urllib2:https://docs.python.org/2/library/urllib2.html

httplib:https://docs.python.org/2/library/httplib.html?highlight=httplib

requests:http://www.python-requests.org/en/latest/

作者:Believer007
来源链接:https://www.cnblogs.com/landhu/p/5104628.html

标签: HTTP

“python中http请求方法库汇总” 的相关文章

HTTP 请求报文 响应报文

HTTP 请求报文 响应报文

引言   超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议。所有的WWW文件都必须遵守这个标准。设计HTTP最...

http请求方法总结

http请求方式总结: get请求:默认的请求方式,返回页面具体内容,参数会显示在地址栏中 head请求:类似于get'请求,但没有返回页面具体内容...

Postman—Http请求模拟工具

Postman—Http请求模拟工具

最近部门让我测试一个短信平台的接口,然后网上搜了一 搜,刚好找到了这个接口 一、Postman说明   Postman是一种网页调试与...

C# 监听HTTP请求

先把代码放在这里,下面再详细解说:  using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Orac...

Http请求-okhttp3基本用法

Http请求-okhttp3基本用法

简介 HTTP是现代应用常用的一种交换数据和媒体的网络方式,高效地使用HTTP能让资源加载更快,节省带宽。OkHttp是一个高效的HTTP客户端,它有以下默认特性:...

一次完整的HTTP网络请求过程详解

0.  前言 从我们在浏览器的地址栏输入http://blog.csdn.net/seu_calvin后回车,到我们看到该博客的主页,这中间经历了什么呢?简单地...

HTTP请求方式中8种请求方法。

转自:http://www.pinlue.com/article/2020/09/0821/3811211887515.html 作者:銨靜菂等芐紶 来源链接:h...

WCF服务支持HTTP(get,post)方式请求例子

WCF服务支持HTTP(get,post)方式请求例子

方式一: /// <summary> /// Http Get请求 /// </summary> /// <...

HTTP请求方法

HTTP请求方法 根据HTTP标准,HTTP请求可以使用多种请求方法。 HTTP1.0定义了三种请求方法: GET, POST 和 H...

[html] 点击html界面button,实现http请求发送,原界面不做跳转

预期实现的功能: 1. 点击界面上的一个按钮,出发JavaScript动作 2. 在JavaScript中,实现http请求发送 3. 原有界面保持不变(即...