Skip to content

Instantly share code, notes, and snippets.

@alswl
Created February 9, 2017 09:47
Show Gist options
  • Save alswl/d870f510e29dc1bc4be9367fd8bea1cd to your computer and use it in GitHub Desktop.
Save alswl/d870f510e29dc1bc4be9367fd8bea1cd to your computer and use it in GitHub Desktop.
one function simple requests for Python
#!/usr/bin/env python
# coding: utf-8
import urllib
import urllib2
def _request(path, params=None, method='GET', data=None, headers=None):
params = params or {}
headers = headers or {}
if params:
url = path + '?' + urllib.urlencode(params)
else:
url = path
request = urllib2.Request(url, data, headers)
request.get_method = lambda: method
response = urllib2.urlopen(request)
return response.read()
# usage:
_request(path='http://www.google.com', {'a': 'A'}, method='GET', headers={'User-Agent': 'Browser'})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment