如何用Python写一个http post请求?表单传参是json格式的

张冬洪 发布于 2013/04/15 16:09
阅读 22K+
收藏 1
加载中
0
张冬洪

谢谢上面各位:下面是我自己按照自己的需求写的,贴出来作个参考:

'''
Created on 2013-4-15
@author : zdh
'''
#!/usr/bin/env python
#-*- coding: utf-8 -*-


import httplib
import json
conn = httplib.HTTPConnection("localhost:8080")
headers = {"Content-type":"application/json"} #application/x-www-form-urlencoded
params = ({"bindHyCardInfo":{"mobileNo":"1881026xxxx","userId":"2","cardno":"7926279367963021","cardpasswd":"xxxxxxxxxxxxxxx","ip":"127.0.0.1"},"header":{"version":"1.0.1","from":"1000","to":"2000","tid":"7926279367963021","time":"12312","token":"SEW342WEER2342","ext":""}})
conn.request("POST", "/core-oper/rest/bindCard", json.JSONEncoder().encode(params), headers)
response = conn.getresponse()
data = response.read()
if response.status == 200:
print 'success'
print data
else:
print 'fail'
conn.close() 

0
Yisen
Yisen
用httplib
0
王大麻烦
王大麻烦
python-requests 
0
宁宁123韩
宁宁123韩

"""我只是意思意思一下写了点代码  不完整 但是大概意思应该体现出来了 一些参数是模拟sina weibo登陆的  我有空就去贴出全部代码 以前也写过个分享了 然后sina改了加密算法  我就又写了一个”“”
session = requests.Session()
url_prelogin = "xxxxx"

resp = session.get(url_prelogin)
json_data = re.search('\((.*)\)', resp.content).group(1)
data = json.loads(json_data)
servertime = data['servertime']
nonce = data['nonce']
pubkey = data['pubkey']
rsakv = data['rsakv']

postdata = {
    'entry': 'weibo',
    'gateway': '1',
    'from': '',
    'savestate': '7',
    'userticket': '1',
    'ssosimplelogin': '1',
    'vsnf': '1',
    'vsnval': '',
    'su': su,
    'service': 'miniblog',
    'servertime': servertime,
    'nonce': nonce,
    'pwencode': 'rsa2',
    'sp': sp,
    'encoding': 'UTF-8',
    'url': 'http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack',
    'returntype': 'META',
    'rsakv' : rsakv,
}

resp = session.post(url_login,data=postdata)
print resp.content


0
用户名已存在
用户名已存在
urllib json 设置头 
返回顶部
顶部