python之简单的get和post请求
1.json 模块提供了一种很简单的方式来编码和解码JSON数据。 其中两个主要的函数是 json.dumps() 和 json.loads() , 要比其他序列化函数库如pickle的接口少得多。 下面演示如何将一个Python数据结构转换为JSON:
import json
data = {
'name' : 'ACME',
'shares' : 100,
'price' : 542.23
}
json_str = json.dumps(data)
下面演示如何将一个JSON编码的字符串转换回一个Python数据结构:
data = json.loads(json_str)
2.简单的get和post请求,使用import requests
import requests
response = requests.get('http://httpbin.org/get')
print(response.text)
#通过在发送post请求时添加一个data参数,这个data参数可以通过字典构造成
import requests data = {
"name":"zhaofan",
"age":23
}
response = requests.post("http://httpbin.org/post",data=data)
print(response.text)
3.GET方法,并且自定义header
# -* - coding: UTF-8 -* -
import urllib2 request = urllib2.Request("http://www.baidu.com/")
request.add_header('content-TYPE', 'application/x-www-form-urlencoded')
response = urllib2.urlopen(request)
print response.getcode()
print response.geturl()
print response.read()
POST方法,并且自定义header
# -* - coding: UTF-8 -* -
import urllib2
import urllib request = urllib2.Request("http://passport.cnblogs.com/login.aspx")
request.add_header('content-TYPE', 'application/x-www-form-urlencoded')
data={"tbUserName":"test_username", "tbPassword":"test_password"} response = urllib2.urlopen(request, urllib.urlencode(data))
print response.getcode()
print response.geturl()
print response.read()
4.实际测试脚本编写
# coding:utf-8
import json
import urllib2
import requests class AddScores:
def __init__(self):
pass def getToken(self): # 获取token值
url1 = 'xxxxx'#url
r1 = requests.get(url1)
self.tokenObj = json.loads(r1.text)#解码JSON数据 if self.tokenObj["result"] == "success":
print self.tokenObj["token"]
else:
print "failed"
return self.tokenObj["token"] def personMess(self): # 获取个人信息
url2 = 'xxx' + self.getToken()
r2 = requests.post(url2)
print r2.text def addSco(self,resId): # 添加分数
data = {
"memberId": "xxx",
"orgCode": "xxx",
"resourceId": resId,#传参,传resourceId
"configName": "wsp", "resourceType": "wsp"
} print "添加分数的请求参数:"
print json.dumps(data)#编码JSON headers = {'Content-Type': 'application/json'}
url3 = 'xxx' + self.getToken()
re3 = urllib2.Request(url=url3, headers=headers, data=json.dumps(data))
response = urllib2.urlopen(re3)
print response.read()
5.读写TXT文件
#coding:utf-8
import time from Demo2.token import AddScores class ResId:
def getResId(self):
file=open('xxxx')
# a=file.read()
# print a
lId= file.readline()
lId=lId.strip(',\n') while lId != '':#逐行读取数据
print lId
addScores = AddScores()
addScores.getToken()
addScores.personMess()
addScores.addSco(lId) time.sleep(68) lId = file.readline()
print "=============================" ResId().getResId()
详情参考https://www.cnblogs.com/zhaof/p/6915127.html http://python3-cookbook.readthedocs.io/zh_CN/latest/c06/p02_read-write_json_data.html
python之简单的get和post请求的更多相关文章
- python web.py实现简单的get和post请求
使用web.py框架,实现简单的get和post请求: py文件名:mytest.py import web urls = ( '/', 'hello' ) app = web.application ...
- Python简单的get和post请求
1.json 模块提供了一种很简单的方式来编码和解码JSON数据. 其中两个主要的函数是 json.dumps() 和 json.loads() , 要比其他序列化函数库如pickle的接口少得多. ...
- Python 实现简单的 Web
简单的学了下Python, 然后用Python实现简单的Web. 因为正在学习计算机网络,所以通过编程来加强自己对于Http协议和Web服务器的理解,也理解下如何实现Web服务请求.响应.错误处理以及 ...
- Python Thrift 简单示例
本文基于Thrift-0.10,使用Python实现服务器端,使用Java实现客户端,演示了Thrift RPC调用示例.Java客户端提供两个字符串参数,Python服务器端计算这两个字符串的相似度 ...
- python实现简单工厂模式
python实现简单工厂模式 模式定义 简单工厂模式(Simple Factory Pattern):又称为静态工厂方法(Static Factory Method)模式,它属于类创建型模式.在简单工 ...
- Python 利用Python编写简单网络爬虫实例3
利用Python编写简单网络爬虫实例3 by:授客 QQ:1033553122 实验环境 python版本:3.3.5(2.7下报错 实验目的 获取目标网站“http://bbs.51testing. ...
- Python 利用Python编写简单网络爬虫实例2
利用Python编写简单网络爬虫实例2 by:授客 QQ:1033553122 实验环境 python版本:3.3.5(2.7下报错 实验目的 获取目标网站“http://www.51testing. ...
- Websocket - Websocket原理(握手、解密、加密)、基于Python实现简单示例
一.Websocket原理(握手.解密.加密) WebSocket协议是基于TCP的一种新的协议.WebSocket最初在HTML5规范中被引用为TCP连接,作为基于TCP的套接字API的占位符.它实 ...
- Python爬虫简单入门及小技巧
刚刚申请博客,内心激动万分.于是为了扩充一下分类,随便一个随笔,也为了怕忘记新学的东西由于博主十分怠惰,所以本文并不包含安装python(以及各种模块)和python语法. 目标 前几天上B站时看到一 ...
随机推荐
- vsCode 添加浏览器调试和js调试的方法总结
vsCode 添加浏览器调试和js调试的方法 1.直接按F5可以调试的方法或者点击运行按钮(可以直接运行html文件或者js文件) 在launch.json文件中的配置如下: { " ...
- OpenSAML2.X 在SSO系统中的应用
背景 年底的时候有机会开发一个SPA(单页面应用)的项目,那时候须要用到票据的方式能够用Cookie的方式来登录.当是想到了OpenID或者是CAS的方式来做统一认证中心.后来一个安全界的大牛推荐让我 ...
- vue组件调用(全局调用和局部调用)
当用vue-cli创建一个项目后, 创建项目的方法: https://www.cnblogs.com/fps2tao/p/9376847.html 编写了组件怎么,在其他组件中调用了? 组件listB ...
- AngularJS 路由:ng-route 与 ui-router
AngularJS的ng-route模块为控制器和视图提供了[Deep-Linking]URL. 通俗来讲,ng-route模块中的$routeService监测$location.url()的变化, ...
- Android Studio 更新gradle插件
今天更新了CentOS, 更新了java版本. 然后gradle跪了..... 不吐槽java版本的兼容性问题了.... 反正有他自己的理由.... 那么就更新gradle咯.... 下面是方法... ...
- js基本知识3
1. 函数 function 函数的声明 函数的 调用 函数的传参 2. 函数返回值 Return 返回结果 返回给函数 Id 函数 function $id(id) { return documen ...
- [Spring Framework]学习笔记--@Component等stereotype的基础
在继续讲解Spring MVC之前,需要说一下常用的几个用来标记stereotype的annotation. @Component,@Controller,@Repository,@Service. ...
- python walk函数
os.walk方法 import os for i in os.walk(r'C:\Users\jack\Desktop\test\3_语文语文版七年级上册\1_一单元'): print(i[0]) ...
- 一款基于css3鼠标经过圆形旋转特效
今天给大家分享一款基于css3鼠标经过圆形旋转特效.当鼠标经过的时候图片边框颜色旋转,图片显示详情.该实例适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗 ...
- spring配置:context:property-placeholder 读取配置文件信息 在配置文件中使用el表达式填充值
spring将properties文件读取后在配置文件中直接将对象的配置信息填充到bean中的变量里. 原本使用PropertyPlaceholderConfigurer类进行文件信息配置.Prope ...