• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python中http请求方法库汇总

Python中http请求方法库汇总

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了方法库,决策支持系统的方法库,以下 属于方法库,方法库前言,python http请求等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

最近在使用python做接口测试,发现python中http请求方法有许多种,今天抽点时间把相关内容整理,分享给大家,具体内容如下所示:

一、python自带库----urllib2

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

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

简单的get请求

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

这就是最简单的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() 
</div>

简单的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() 

</div>

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

三、第三方库--requests

发请get请求超级简单:

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

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

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

也很简单。

再看看如果要认证:

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

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

python中的http请求

import urllib
params = urllib.urlencode({key:value,key:value})
resultHtml = urllib.urlopen('[API or 网址]',params)
result = resultHtml.read()
print result
</div>

</div>

您可能想查找下面的文章:

  • Python中http请求方法库汇总

相关文章

  • Python中的anydbm模版和shelve模版使用指南
  • Python调用ctypes使用C函数printf的方法
  • Python 专题一 函数的基础知识
  • python备份文件的脚本
  • Python中的ceil()方法使用教程
  • Python 爬虫学习笔记之多线程爬虫
  • Python中的异常处理相关语句基础学习笔记
  • Python Socket编程详细介绍
  • Python的装饰器模式与面向切面编程详解
  • python读取注册表中值的方法

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 安装Python的web.py框架并从hello world开始编程
    • 再谈Python中的字符串与字符编码(推荐)
    • Python中内置的日志模块logging用法详解
    • Python处理RSS、ATOM模块FEEDPARSER介绍
    • Python中的map、reduce和filter浅析
    • Python中Django框架利用url来控制登录的方法
    • Python的Flask框架及Nginx实现静态文件访问限制功能
    • django 文件上传功能的相关实例代码(简单易懂)
    • Python中的包和模块实例
    • Python实现保证只能运行一个脚本实例

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有