Download and decompress gzipped file in memory?

You need to seek to the beginning of compressedFile after writing to it but before passing it to gzip.GzipFile(). Otherwise it will be read from the end by gzip module and will appear as an empty file to it. See below: #! /usr/bin/env python import urllib2 import StringIO import gzip baseURL = “https://www.kernel.org/pub/linux/docs/man-pages/” filename = … Read more

Limiting number of processes in multiprocessing python

The simplest way to limit number of concurrent connections is to use a thread pool: #!/usr/bin/env python from itertools import izip, repeat from multiprocessing.dummy import Pool # use threads for I/O bound tasks from urllib2 import urlopen def fetch(url_data): try: return url_data[0], urlopen(*url_data).read(), None except EnvironmentError as e: return url_data[0], None, str(e) if __name__==”__main__”: pool … Read more

urllib2 HTTP Error 400: Bad Request

The reason that “the dog” returns a 400 Error is because you aren’t escaping the string for a URL. If you do this: import urllib, urllib2 quoted_query = urllib.quote(query) host=”http://www.bing.com/search?q=%s&go=&qs=n&sk=&sc=8-13&first=%s” % (quoted_query, page) req = urllib2.Request(host) req.add_header(‘User-Agent’, User_Agent) response = urllib2.urlopen(req) It will work. However I highly suggest you use requests instead of using urllib/urllib2/httplib. … Read more

Submitting to a web form using python

If you want to pass q as a parameter in the URL using requests, use the params argument, not data (see Passing Parameters In URLs): r = requests.get(‘http://stackoverflow.com’, params=data) This will request https://stackoverflow.com/?q=%5Bpython%5D , which isn’t what you are looking for. You really want to POST to a form. Try this: r = requests.post(‘https://stackoverflow.com/search’, data=data) … Read more

Python POST binary data

Basically what you do is correct. Looking at redmine docs you linked to, it seems that suffix after the dot in the url denotes type of posted data (.json for JSON, .xml for XML), which agrees with the response you get – Processing by AttachmentsController#upload as XML. I guess maybe there’s a bug in docs … Read more