測試AppsScript使用的python程式
這裡感謝Taylor提供的幫助
python 3.5.2 版本
程式碼如下:#!/usr/bin/python3
# -*- encoding: utf-8 -*-
try:
# for python 3.5.2
import urllib.parse
import urllib.request
except AttributeError:
import urllib
query_method = 0
test_url = "你的測試URL"
param_obj = {
"m":"123456",
}
if 0 == query_method:
# POST method
form_data = urllib.parse.urlencode(param_obj).encode('utf-8')
req = urllib.request.Request(test_url, form_data)
print('query url:', test_url)
with urllib.request.urlopen(req) as f:
status = f.status
resp = f.read().decode('utf-8')
elif 1 == query_method:
# GET method
params = urllib.parse.urlencode(param_obj)
req_url = "%s?%s" % (test_url, params)
print('query url:', req_url)
with urllib.request.urlopen(req_url) as f:
status = f.status
resp = f.read().decode('utf-8')
else:
raise Exception('unknown query_method:%d' % query_method)
print('status:',status)
print('response:',resp)
python 2.7.12版本
需要先裝requests套件pip3 install requests
程式碼如下:#!/usr/bin/python
# -*- encoding: utf-8 -*-
import requests
import urllib
query_method = 0
test_url = "你的測試URL";
param_obj = {
"m": "123456",
}
if 0 == query_method:
# POST method
print 'query url:', test_url
resp = requests.post(test_url, data=param_obj, allow_redirects=True, verify=True)
elif 1 == query_method:
# GET method
params = urllib.urlencode(param_obj)
req_url = "%s?%s" % (test_url, params)
print 'query url:', req_url
resp = requests.get(req_url, allow_redirects=True, verify=True)
print 'status:', resp.status_code
print 'response:', resp.content
留言