Google+ Followers

2014年8月28日 星期四

[Python] Google App Engines 發送 POST 與 JSON-RPC

初始資料:

url = 'http://server/cgi'
data = {
"arg1" : "val1",
"arg2" : "val2"
}


發送 POST:

import urllib
from google.appengine.api import urlfetch
post_data = urllib.urlencode(data)
response = urlfetch.fetch(url=url,payload=post_data,method=urlfetch.POST,headers={'Content-Type': 'application/x-www-form-urlencoded'})


發送 JSONRPC:

import urllib2
headers = {
"Content-Type": "application/json"
}
post_data = json.dumps(data)
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req).read()

沒有留言:

張貼留言