How to fetch a non-ascii url with urlopen?

Strictly speaking URIs can’t contain non-ASCII characters; what you have there is an IRI. To convert an IRI to a plain ASCII URI: non-ASCII characters in the hostname part of the address have to be encoded using the Punycode-based IDNA algorithm; non-ASCII characters in the path, and most of the other parts of the address … Read more

Python’s `urllib2`: Why do I get error 403 when I `urlopen` a Wikipedia page?

Wikipedias stance is: Data retrieval: Bots may not be used to retrieve bulk content for any use not directly related to an approved bot task. This includes dynamically loading pages from another website, which may result in the website being blacklisted and permanently denied access. If you would like to download bulk content or mirror … Read more

Python Requests: Post JSON and file in single request

See this thread How to send JSON as part of multipart POST-request Do not set the Content-type header yourself, leave that to pyrequests to generate def send_request(): payload = {“param_1”: “value_1”, “param_2”: “value_2”} files = { ‘json’: (None, json.dumps(payload), ‘application/json’), ‘file’: (os.path.basename(file), open(file, ‘rb’), ‘application/octet-stream’) } r = requests.post(url, files=files) print(r.content)

Handling urllib2’s timeout? – Python

There are very few cases where you want to use except:. Doing this captures any exception, which can be hard to debug, and it captures exceptions including SystemExit and KeyboardInterupt, which can make your program annoying to use.. At the very simplest, you would catch urllib2.URLError: try: urllib2.urlopen(“http://example.com”, timeout = 1) except urllib2.URLError, e: raise … Read more

How do I send a custom header with urllib2 in a HTTP Request?

Not quite. Creating a Request object does not actually send the request, and Request objects have no Read() method. (Also: read() is lowercase.) All you need to do is pass the Request as the first argument to urlopen() and that will give you your response. import urllib2 request = urllib2.Request(“http://www.google.com”, headers={“Accept” : “text/html”}) contents = … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)