Skip to content

Instantly share code, notes, and snippets.

@easonhan007
Created December 28, 2013 08:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save easonhan007/8157364 to your computer and use it in GitHub Desktop.
Save easonhan007/8157364 to your computer and use it in GitHub Desktop.
how to use python to send request via get and post
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