首页 > 编程语言 >Python中http请求方法库汇总

Python中http请求方法库汇总

时间:2023-06-11 17:31:38浏览次数:41  
标签:http Python 汇总 urllib python print post conn


最近在使用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中的http请求 

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

标签:http,Python,汇总,urllib,python,print,post,conn
From: https://blog.51cto.com/u_6186189/6458382

相关文章

  • Python爬虫
    目录PythonSpider第一章爬虫入门1.1爬虫概述1.1.1爬虫原理1.1.2爬虫分类1.1.3爬虫应用1.2爬虫流程1.2.1爬取网页1.2.2解析网页1.2.3存储数据1.3爬虫协议1.3.1Robots协议1.3.2robots.txt文件简介1.3.3robots.txt文件详解1.3.4爬虫准则1.4爬虫环境1.4.1原生Python+......
  • 实验6 turtle绘图与python库应用编程体验
    task1_1代码:fromturtleimport*defmove(x,y):'''画笔移动到坐标(x,y)处'''penup()goto(x,y)pendown()defdraw(n,size=100):'''绘制边长为size的正n变形'''foriinrange(n):......
  • Redis学习笔记4-脚本、持久化和集群 Redis学习笔记1-基础命令及数据结构: http://blog.
        Redis学习笔记4-脚本、持久化和集群Redis学习笔记1-基础命令及数据结构:http://blog.guoyb.com/2016/07/21/learn-redis-basic-commands/Redis学习笔记2-事务与过期时间:http://blog.guoyb.com/2016/08/23/learn-redis-adv/Redis学习笔记3-排序与消息通知:http://blog......
  • Python中的logging模块
    官方文档基本用法下面的代码展示了logging最基本的用法。#-*-coding:utf-8-*-importloggingimportsys#获取logger实例,如果参数为空则返回rootloggerlogger=logging.getLogger("AppName")#指定logger输出格式formatter=logging.Formatter('%(ascti......
  • Centos 7.4+ 通过anaconda 安装Python3.10
    做记录,在centos里安装3.10版本时,老是报错ssl。或者一些其他问题,做个记录吧。大概用了2天才弄好,主业不是运维所以不太了解在https://www.anaconda.com/官网下载安装,此处自己根据系统、根据版本,自己安装下载地址:https://www.anaconda.com/download#downloads安装好后condai......
  • 实验6 turtle绘图与python库应用编程体验
    实验任务1task1_1.py程序源码:1fromturtleimport*23defmove(x,y):#画笔移动到坐标(x,y)处4penup()5goto(x,y)6pendown()78defdraw(n,size=100):#绘制边长为size的正n变形9foriinrange(n):10forward(size)11......
  • 优秀storm博客汇总
    http://aiku.me/bar/10670754http://xumingming.sinaapp.com/756/twitter-storm-drpc/......
  • python: Decorators
      #装饰器defprintpy(func):definner_func():func()print("hellopython!GeovinDu")returninner_func#@装饰器@printpydefprinthello():print("helloworld!")#调用printhello()'''De......
  • Python modbus_tk 库源码分析
    modbus_tk源代码分析前言modbus_tcp协议是工业项目中常见的一种基于TCP/IP协议的设备数据交互协议。作为TCP/IP协议的上层协议,modbus_tcp协议涉及到两个概念:client和server。但更标准的叫法应该是master和slave。Slave:TCP/IP协议中的server方Master:TCP/IP协......
  • 实验六 turtle绘图与python库应用编程体验
    1fromturtleimport*234defmove(x,y):5penup()6goto(x,y)7pendown()8910defdraw(n,size=100):11foriinrange(n):12fd(size)13left(360/n)141516defmain():17pensize(2)18pen......