urllib

How can urllib2 / httplib talk HTTP 1.1 for HTTPS connections via a Squid proxy?

南楼画角 提交于 2019-12-11 02:25:14
问题 When I use urllib2 to make a HTTP 1.1 connection via a squid proxy, squid makes a new ongoing connection in HTTP 1.0. How can I persuade Squid to talk 1.1 to the destination server? 回答1: After dealing with this problem for an entire afternoon, i found the solution. So please excuse me answering my own question, but it would be great if someone else finds this useful and it saves them the pain. In order to get Squid to have a HTTP 1.1 conversation with the destination server, the original

HTTP Error 403: Forbidden while downloading file using urllib

拥有回忆 提交于 2019-12-11 01:31:09
问题 I have this line of code: urllib.request.urlretrieve('http://lolupdater.com/downloads/LPB.exe', 'LPBtest.exe') , but when I run it, it throws an error urllib.error.HTTPError: HTTP Error 403: Forbidden . 回答1: That looks to be an actual HTTP 403: Forbidden error. Python urllib throws the exception when it encounters an HTTP status code (documented here). 403 in general means: "The server understood the request, but is refusing to fulfill it." You will need to add HTTP headers to identify

Python “IOError: [Errno 21] Is a directory: '/home/thomasshera/Pictures/Star Wars'” [closed]

北战南征 提交于 2019-12-11 00:42:53
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . NOTE: the first part of "code" isn't code, it's error message from terminal, SE made me format it that way. Linux machine using Python 2.7.6 I have download automator which is supposed to take clipboard contents (this works) and download them (this doesn't work). Here is error message: Traceback (most recent

how to do urlopen over ipv4 by default

不想你离开。 提交于 2019-12-10 22:46:26
问题 What is the way to do urlopen in python such that even if the underlying machine has ipv6 networking enabled, the request is sent via ipv4 instead of ipv6? 回答1: I had a look into the source code. Unfortunately, urllib.urlopen() seems to use httplib.HTTP() , which doesn't even allow setting a source address. urllib2.urlopen() uses httplib.HTTPConnection() which you could inherit from and create a class which by default sets a source address '0.0.0.0' instead of '' . Then you could somehow

Mock exception raised in function using Pytest

大憨熊 提交于 2019-12-10 21:31:33
问题 I have the following function and it is a generic function which will make API call based on the input hostname and data. It will construct http request to make API and will return the response. This function will throw four types of exception(invalid URL, timeout, auth error and status check). How can I Mcok and Test the exception raised in API call using pytest? Which will be the best method to test the exceptions raised from API call? import ssl import urllib import urllib.request import

gaierror: [Errno -2] Name or service not known

流过昼夜 提交于 2019-12-10 20:41:19
问题 def make_req(data, url, method='POST') params = urllib.urlencode(data) headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain", } conn = httplib.HTTPSConnection(url) conn.request(method, url, params, headers) response = conn.getresponse() response_data = response.read() conn.close() But it is throwing: in create_connection for res in getaddrinfo(host, port, 0, SOCK_STREAM): gaierror: [Errno -2] Name or service not known What is the reason ? What is this error?

Play the contents of a sound retrieved from an url?

独自空忆成欢 提交于 2019-12-10 20:01:07
问题 I am retrieving the sound from: http://translate.google.com/translate_tts and writing it to a WAV file, when i double-click the file the sound plays ok, but when i use the WAVE module from python to open it, it gives me this error: wave.Error: file does not start with RIFF id I want to know if there is a way for openning this file, or if it is possible to play the sound without writing it before. Here is the relevant code: url = "http://translate.google.com/translate_tts?tl=%s&q=%s" % (lang,

Google http://maps.google.com/maps/geo query with non-english characters

徘徊边缘 提交于 2019-12-10 17:53:11
问题 I'm creating a Python (using urllib2 ) parser of addresses with non-english characters in it. The goal is to find coordinates of every address. When I open this url in Firefox : http://maps.google.com/maps/geo?q=Czech%20Republic%2010000%20Male%C5%A1ice&output=csv it is converted (changes in address box) to http://maps.google.com/maps/geo?q=Czech Republic 10000 Malešice&output=csv and returns 200,6,50.0865113,14.4918052 which is a correct result. However, if I open the same url (encoded, with

Wrap an io.BufferedIOBase such that it becomes seek-able

爷,独闯天下 提交于 2019-12-10 17:29:58
问题 I was trying to craft a response to a question about streaming audio from a HTTP server, then play it with PyGame. I had the code mostly complete, but hit an error where the PyGame music functions tried to seek() on the urllib.HTTPResponse object. According to the urlib docs, the urllib.HTTPResponse object (since v3.5) is an io.BufferedIOBase . I expected this would make the stream seek()able, however it does not. Is there a way to wrap the io.BufferedIOBase such that it is smart enough to

How to formally insert URL space (%20) using Python? [duplicate]

坚强是说给别人听的谎言 提交于 2019-12-10 15:35:51
问题 This question already has answers here : How to urlencode a querystring in Python? (12 answers) Closed 4 years ago . When a mutli-word search term is entered in ebay, the resultant URL looks something like (for example "demarini cf5 cf12"): http://www.ebay.com/sch/i.html?_from=R40&_sacat=0&_nkw=demarini%20cf5%20cfc12 I wish to construct this URL in Python so it can be accessed directly. So it's case of concatenating the base URL: http://www.ebay.com/sch/i.html?_from=R40&_sacat=0&_nkw= ...