Python POST Json数据到Http


》安装Python

》cmd terminal 安装requests模块,command: pip install requests

》Python 脚本:此列是post “json.json”的样本到“url ”

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2
import urllib
import cookielib
import json
import httplib
import re
import requests
s=requests.session()
headers = {'Content-Type':'application/json'}
url = 'http://localhost:7654/hermes/command'   #Post url
fo = open("C:\Json_Post\json.json","rb")  #需要post的json数据
values = fo.read()
try:
r = s.post(url,data=values,headers = headers)
except BaseException:
print"Error:connection"
else:
print r.text
fpost = open("C:\Json_Post\Post\post_result.txt","w")
fpost.write(r.text)
fpost.close()

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM