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请求的更多相关文章

  1. python web.py实现简单的get和post请求

    使用web.py框架,实现简单的get和post请求: py文件名:mytest.py import web urls = ( '/', 'hello' ) app = web.application ...

  2. Python简单的get和post请求

    1.json 模块提供了一种很简单的方式来编码和解码JSON数据. 其中两个主要的函数是 json.dumps() 和 json.loads() , 要比其他序列化函数库如pickle的接口少得多. ...

  3. Python 实现简单的 Web

    简单的学了下Python, 然后用Python实现简单的Web. 因为正在学习计算机网络,所以通过编程来加强自己对于Http协议和Web服务器的理解,也理解下如何实现Web服务请求.响应.错误处理以及 ...

  4. Python Thrift 简单示例

    本文基于Thrift-0.10,使用Python实现服务器端,使用Java实现客户端,演示了Thrift RPC调用示例.Java客户端提供两个字符串参数,Python服务器端计算这两个字符串的相似度 ...

  5. python实现简单工厂模式

    python实现简单工厂模式 模式定义 简单工厂模式(Simple Factory Pattern):又称为静态工厂方法(Static Factory Method)模式,它属于类创建型模式.在简单工 ...

  6. Python 利用Python编写简单网络爬虫实例3

    利用Python编写简单网络爬虫实例3 by:授客 QQ:1033553122 实验环境 python版本:3.3.5(2.7下报错 实验目的 获取目标网站“http://bbs.51testing. ...

  7. Python 利用Python编写简单网络爬虫实例2

    利用Python编写简单网络爬虫实例2 by:授客 QQ:1033553122 实验环境 python版本:3.3.5(2.7下报错 实验目的 获取目标网站“http://www.51testing. ...

  8. Websocket - Websocket原理(握手、解密、加密)、基于Python实现简单示例

    一.Websocket原理(握手.解密.加密) WebSocket协议是基于TCP的一种新的协议.WebSocket最初在HTML5规范中被引用为TCP连接,作为基于TCP的套接字API的占位符.它实 ...

  9. Python爬虫简单入门及小技巧

    刚刚申请博客,内心激动万分.于是为了扩充一下分类,随便一个随笔,也为了怕忘记新学的东西由于博主十分怠惰,所以本文并不包含安装python(以及各种模块)和python语法. 目标 前几天上B站时看到一 ...

随机推荐

  1. MyEclipse连接sqlserver2008具体流程

    参照这里: 图形连接  http://wenku.baidu.com/view/f50838086c85ec3a87c2c53a.html 还有查看的是这个:   2. 重新用Window验证方式登陆 ...

  2. android TextView异常换行层次不齐的问题

    关于TextView异常换行,参差不齐问题分析:每行的开头的第一个字符如果是字母,数字,标点符号以及特殊字符 可能是由于都是半角字符,所以需要转化成全角字符 解决方法是: public String ...

  3. 【phpstorm】破解安装

    1.使用前修改C:\windows\system32\Driver\hosts文件,将“0.0.0.0 account.jetbrains.com”添加到hosts文件中. 2. 浏览器打开 http ...

  4. PHP-数据库长连接mysql_pconnect的细节

    PHP的MySQL持久化连接,美好的目标,却拥有糟糕的口碑,往往令人敬而远之.这到底是为啥么.近距离观察后发现,这家伙也不容易啊,要看Apache的脸色,还得听MySQL指挥. 对于作为Apache模 ...

  5. cocopods卸载、安装、重装等问题解决(转)

    今日在升级af库的时候,头脑发热把cocopods给卸载了,然后重装就出现了一些问题,主要是Mac ox s升级至10.11之后,好多命令都和以前不一样了,现在重新总结其安装步骤,如下: 一.全新安装 ...

  6. JS 校验,检测,验证,判断函数集合

    http://jc-dreaming.iteye.com/blog/754690 /**  *判断对象是否为空 *Check whether string s is empty.  */  funct ...

  7. 零基础学python-5.4 数字精度与复数

    1.整数精度 这里我们对照一下python2.7与python3.4的整数精度 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/ ...

  8. C# ManualResetEvent

    文章转载自:C# ManualResetEvent ManualResetEvent被用于在两个或多个线程间进行线程信号发送. 多个线程可以通过调用ManualResetEvent对象的WaitOne ...

  9. 统计重复字符串 如 eeefffkkkhjk 得到如下结果 3e3f3khjk;

    //统计重复字符串 如 eeefffkkkhjk 得到如下结果 3e3f3khjk; string zipStr = Console.ReadLine(); var charList = zipStr ...

  10. MySQL主从不一致的几种故障总结分析、解决和预防

    (1).主从不一致故障,从库宕机,从库启动后重复写入数据报错解决与预防:relay_log_info_repository=TABLE(InnoDB)参数解释说明:若relay_log_info_re ...