Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
curl to requests
import re
import pprint
import requests
test = """
curl '' \
-H 'Pragma: no-cache' \
-H 'Origin:' \
-H 'Accept-Encoding: gzip,deflate,sdch' \
-H 'Host:' \
-H 'Accept-Language: en-US,en;q=0.8,fr;q=0.6' \
-H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/30.0.1599.114 Chrome/30.0.1599.114 Safari/537.36' \
-H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' \
-H 'Accept: */*' \
-H 'Cache-Control: no-cache' \
-H 'X-Requested-With: XMLHttpRequest' \
-H 'Connection: keep-alive' \
-H 'Referer:' \
--data 'data={"amount":"1","currencyFrom":"EUR","currencyTo":"USD"}' --compressed
def parse_header(header):
name, value = header.split(": ")
if name not in TRIMMABLE_HEADERS:
return name, value
return None
def parse_curl(command):
rx = re.compile("'(.*?)'")
res = rx.findall(command)
url = res[0]
headers = dict(x for x in map(parse_header, res[1:-1]) if x)
data = res[-1]
return url, headers, data
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--method", default="GET")
args = parser.parse_args()
args.method = args.method.lower()
url, headers, data = parse_curl(test)
print "REQUEST"
print "url = '%s'" % url
print "headers = %s" % pprint.pformat(headers)
print "data = '%s'" % data
print "requests.%s(url, headers=headers, data=data)" % args.method
print "RESPONSE"
req = getattr(requests, args.method)(url, headers=headers, data=data)
print req.text
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.