在Python3下,用urllib发起GET请求或POST请求
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 7 10:20:09 2017
@author: qcy
"""
from urllib.request import urlopen
import json
# GET
url = 'http://127.0.0.1:8889'
content = {'a':3,'s':4}
data_json = json.dumps(content)
print(data_json)
web = urlopen(url, bytes(data_json,encoding='utf-8'))
rawtext = web.read()
sss = rawtext.decode('utf8')
print(sss)
# POST
url = 'http://127.0.0.1:8889?name=qcy&age=24'
web = urlopen(url)
rawtext = web.read()
sss = rawtext.decode('utf8')
print(sss)