Created
February 9, 2017 09:47
-
-
Save alswl/d870f510e29dc1bc4be9367fd8bea1cd to your computer and use it in GitHub Desktop.
one function simple requests for Python
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
#!/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