-
python3教程之Python中http请求方法库汇总
本站最新发布 Python从入门到精通|Python基础教程
试听地址 https://www.xin3721.com/eschool/python.html
试听地址 https://www.xin3721.com/eschool/python.html
最近在使用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封装的。简单使用如下:
1
2
3
4
5
6
7
8
9
10
|
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请求
1
2
3
4
5
6
7
8
9
|
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请求超级简单:
1
|
print requests.get('http: //localhost :8080).text |
栏目列表
最新更新
python数据库连接池技术总结
python数据库连接池技术总结
成人网站性能提升 20 倍之经验谈 [Python
python动态捕获异常
python 探测网站目录的GUI程序
python实现中文字符繁体和简体中文转换
Python服务器开发 -- 网络基础
python高性能编程方法一
使用python管理Cisco设备
python抓取google搜索结果
基于UDP的服务器端和客户端
再谈UDP和TCP
在socket编程中使用域名
网络数据传输时的大小端问题
socket编程实现文件传输功能
如何优雅地断开TCP连接?
图解TCP四次握手断开连接
详细分析TCP数据的传输过程
图解TCP数据报结构以及三次握手(非常详
TCP协议的粘包问题(数据的无边界性)
Excel数据导入到Sql server
SQL Server like 字段
SQL Server中的LEFT、RIGHT函数
sql server 安装出现需要sqlncli.msi文件,错误
SQL Server学习内容(一)
SQLServer执行大脚本文件时,提示“无法执
数据库敏捷版本控制之3个数据库策略
将select 转为json
SQL Server 创建索引(index)
GROUP BY中的WITH CUBE、WITH ROLLUP原理测试及