Created
December 28, 2013 08:30
-
-
Save easonhan007/8157364 to your computer and use it in GitHub Desktop.
how to use python to send request via get and post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import httplib, json, urllib | |
# get | |
conn = httplib.HTTPConnection('localhost', 5000) | |
conn.request('GET', '/') | |
r1 = conn.getresponse() | |
print r1.status, r1.reason | |
data = r1.read() | |
print json.loads(data) | |
conn.close() | |
# post | |
params = urllib.urlencode({'name': 'rose', 'age': '16', 'sex': 'female'}) | |
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "application/json"} | |
conn = httplib.HTTPConnection('localhost', 5000) | |
conn.request('post', '/add', params, headers) | |
response = conn.getresponse() | |
print response.status, response.reason | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment