.python接口之http请求
python的强大之处在于提供了很多的标准库以及第三库,本文介绍urllib

和第三库的requests。

Urllib 定义了很多函数和类,这些函数和类能够帮助我们在复杂的情况下获取url内容。复杂情况— 基本的和深入的验证, 重定向, cookies 等等

2.requests的GET请求代码如下:

# response=requests.get('http://httpbin.org/get?name=gemey&age=22')

data={
'name':'tom',
'age':20
}
response=requests.get('http://httpbin.org/get', params=data)
# print(response.status_code) # 打印状态码
# print(response.url) # 打印请求url
# print(response.headers) # 打印头信息
# print(response.cookies) # 打印cookie信息
print(response.text) #以文本形式打印网页源码
# print(response.content) #以字节流形式打印

 3.Urllib的GET请求代码如下:

import urllib.request

url='http://www.baidu.com'

response=urllib.request.Request(url=url)

html=urllib.request.urlopen(response)

print(html.getcode())

print(html.headers)

请求结果:

200
Bdpagetype: 1
Bdqid: 0x8356551800064134
Cache-Control: private
Content-Type: text/html
Cxy_all: baidu+e5a8ce572d979b514b736b0cea852f5d
Date: Fri, 08 Jun 2018 01:21:13 GMT
Expires: Fri, 08 Jun 2018 01:20:22 GMT
P3p: CP=" OTI DSP COR IVA OUR IND COM "
Server: BWS/1.1
Set-Cookie: BAIDUID=2B497098824EEEF8A1B592EBC590946D:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: BIDUPSID=2B497098824EEEF8A1B592EBC590946D; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: PSTM=1528420873; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: BDSVRTM=0; path=/
Set-Cookie: BD_HOME=0; path=/
Set-Cookie: H_PS_PSSID=1436_21112_26350_26579_22159; path=/; domain=.baidu.com
Vary: Accept-Encoding
X-Ua-Compatible: IE=Edge,chrome=1
Connection: close
Transfer-Encoding: chunked

Urllib的Post请求,代码:

import urllib.request

import urllib.parse

url='http://www.tuling123.com/openapi/api'

data={"key": "your", "info": '你好'}

data=urllib.parse.urlencode(data).encode('utf-8')

re=urllib.request.Request(url,data)

html=urllib.request.urlopen(re)

print(html.getcode(),html.msg)

print(html.read())

请求结果:

200 OK
b'{"code":40001,"text":"\xe4\xba\xb2\xe7\x88\xb1\xe7\x9a\x84\xef\xbc\x8ckey\xe4\xb8\x8d\xe5\xaf\xb9\xe5\x93\xa6\xe3\x80\x82"}'
[Finished in 0.7s]

requests  post请求:

url='http://www.tuling123.com/openapi/api'

data={"key": "your", "info": '你好'}

response = requests.post('http://www.tuling123.com/openapi/api', data=data)

print(response.status_code) # 打印状态码
print(response.url) # 打印请求url
print(response.headers) # 打印头信息
print(response.cookies) # 打印cookie信息
print(response.text) #以文本形式打印网页源码
print(response.content) #以字节流形式打印

结果:

在来一个:

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

python学习接口测试(二)的更多相关文章

  1. Python学习(二)Python 简介

    Python 简介 官方指南及文档 Python2.7官方指南(中文版):http://pan.baidu.com/s/1dDm18xr Python3.4官方指南(中文版):http://pan.b ...

  2. 编程语言与Python学习(二)

    1.1 流程控制之for循环 1 迭代式循环:for,语法如下 for i in range(10): 缩进的代码块 2 break与continue(同上) 3 循环嵌套 for i in rang ...

  3. python学习笔记(二)、字符串操作

    该一系列python学习笔记都是根据<Python基础教程(第3版)>内容所记录整理的 1.字符串基本操作 所有标准序列操作(索引.切片.乘法.成员资格检查.长度.最小值和最大值)都适用于 ...

  4. (10.1)Python学习笔记二

    1.在项目工程中要模块化测试一个开发的功能,在测试通过后交付给项目组其他人员继续开发.要保证代码开发的性能和效率以及可扩展性. 2.项目工程中的文件夹分类要功能模块明确清晰,在python中引入某一个 ...

  5. python学习(二)

    这几天脑子里一直在想一个应用,想以此来练习python.用一句话来概括这个应用的功能,大致表述是这样:自动采集全省各类公共文化机构网站上新发布的信息,并分类呈现.各类公共文化机构,是指公共图书馆.文化 ...

  6. Python学习之二:Python 与 C 区别

    引自http://www.lxway.com/181844.htm 从开始看Python到现在也有半个多月了,前后看了Python核心编程和Dive into Python两本书.话说半个月看两本,是 ...

  7. Python学习(二十六)—— Django基础一

    转载自:http://www.cnblogs.com/liwenzhou/p/8258992.html 一.Web框架本质 我们可以这样理解:所有的Web应用本质上就是一个socket服务端,而用户的 ...

  8. Python学习(二)——深度学习入门介绍

    课程二:深度学习入门 讲师:David (数据分析工程师) 这门课主要介绍了很多神经网络的基本原理,非常非常基础的了解. 零.思维导图预览:                一.深度神经网络 1.神经元 ...

  9. python学习(二)之turtle库绘图

    今天是三月七号,也就是女生节,或者女神节.不知道你是不是有自己喜欢的女孩子,在这里你可以用turtle库绘制一朵玫瑰花,送给你喜欢的姑娘.(拉到最后有惊喜哦)但在画这朵玫瑰花之前,先来一个基础的图形, ...

随机推荐

  1. codeforces之4.1学习记录

    记录一些之前没见过的代码: #include <bits/stdc++.h> using namespace std; typedef long long ll; #define INF ...

  2. Linux关于用户信息的一些命令

    1.用户增加删除[root@VM_0_13_centos ~]# useradd better407 #添加 better407 用户 [root@VM_0_13_centos ~]# passwd ...

  3. php数组·的方法1-数组统计函数

    /** * 下面是数组统计函数 * * * **/ //count() 数组的长度 print_r(count($arr3)); echo '<hr>'; //max() min() 数组 ...

  4. CentOS 7 修改root 密码

    环境: 1.重启系统在加载内核的地方按e,编辑启动脚本 2.将ro crashkernel = auto 所在地的 ro 替换为 rw init=/sysroot/bin/sh 3.修改完成后用Ctr ...

  5. php spl数据结构

    1.双链表SplDoublyLinkedList 结构如图: 类定义: SplDoublyLinkedList implements Iterator , ArrayAccess , Countabl ...

  6. 封装WebService的APM为Async、Await模式利于Asp.Net页面调用

    Wcf针对Async.Await指令直接可以返回Task<T>结果,但是老旧的系统中还是会有很多是在用Soap的Webservice.直接在Asp.Net页面调用APM方法确实比较麻烦,其 ...

  7. [LeetCode]19. Remove Nth Node From End of List删除链表的倒数第N个节点

    Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...

  8. DOS常见命令

    dir: 显示一个目录中的文件和子目录 md: 创建目录 rd: 删除目录 cd: 进入指定目录 cd..: 退回到上级目录 cd\: 退回到根目录 del: 删除文件 set: 显示.设置.删除cm ...

  9. sqlserver门户设置

    ------ insert by wandz 20180918 门户模板表 start ------set identity_insert oa_portal_template on;begin de ...

  10. Python列表边遍历边修改问题解决方案:alist[:]

    最近在看python,遇到个简单的问题:删除列表中指定的重复元素,发现一些实用并且有趣的东西. 1.错误示范 alist = [1,1,2,2,3,3,2,2,1,1] for i in alist: ...