httplib

How do I use Python's httplib to send a POST to a URL, with a dictionary of parameters?

…衆ロ難τιáo~ 提交于 2019-12-04 00:03:09
I just want a function that can take 2 parameters: the URL to POST to a dictionary of parameters How can this be done with httplib? thanks. TIMEX From the Python documentation : >>> import httplib, urllib >>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0}) >>> headers = {"Content-type": "application/x-www-form-urlencoded", ... "Accept": "text/plain"} >>> conn = httplib.HTTPConnection("musi-cal.mojam.com:80") >>> conn.request("POST", "/cgi-bin/query", params, headers) >>> response = conn.getresponse() >>> print response.status, response.reason 200 OK >>> data = response.read() >>>

Permanent 'Temporary failure in name resolution' after running for a number of hours

被刻印的时光 ゝ 提交于 2019-12-03 17:57:45
问题 After running for a number of hours on Linux, my Python 2.6 program that uses urllib2, httplib and threads, starts raising this error for every request: <class 'urllib2.URLError'> URLError(gaierror(-3, 'Temporary failure in name resolution'),) If I restart the program it starts working again. My guess is some kind of resource exhaustion but I don't know how to check for it. How do I diagnose and fix the problem? 回答1: This was caused by a library's failure to close connections, leading to a

In requests library, how can I avoid “HttpConnectionPool is full, discarding connection” warning?

爷,独闯天下 提交于 2019-12-03 16:14:25
问题 I'm using python requests library with sessions: def _get_session(self): if not self.session: self.session = requests.Session() return self.session And sometimes I'm getting this warning in my logs: [2014/May/12 14:40:04 WARNING ] HttpConnectionPool is full, discarding connection: www.ebi.ac.uk My question is: why this is warning and not an exception? This is the code responsible for this (from http://pydoc.net/Python/requests/0.8.5/requests.packages.urllib3.connectionpool/): def _put_conn

HTTP Requests using a range of IP address on python

落花浮王杯 提交于 2019-12-02 22:34:40
问题 I have a VM as a server with IP address 10.91.55.2. I have another VM which acts as a client having IP address in the range 10.91.56.2......10.91.56.10. I want to write a script that will use all these IP address on the client to send HTTP request to the server (10.91.55.2). I have written a script that sends HTTP requests using the Physical IP address alone. Is there any way to send HTTP Requests from a range of IP address. My OS Is linux. 回答1: This is not a complete answer, but should

HTTP Requests using a range of IP address on python

∥☆過路亽.° 提交于 2019-12-02 13:02:09
I have a VM as a server with IP address 10.91.55.2. I have another VM which acts as a client having IP address in the range 10.91.56.2......10.91.56.10. I want to write a script that will use all these IP address on the client to send HTTP request to the server (10.91.55.2). I have written a script that sends HTTP requests using the Physical IP address alone. Is there any way to send HTTP Requests from a range of IP address. My OS Is linux. Peter Gibson This is not a complete answer, but should provide you with a starting point. You say "I have another VM which acts as a client", so I'll

urllib2 HTTPPasswordMgr not working - Credentials not sent error

混江龙づ霸主 提交于 2019-12-02 05:53:58
问题 The following python curl call has the following successful results: >>> import subprocess >>> args = [ 'curl', '-H', 'X-Requested-With: Demo', 'https://username:password@qualysapi.qualys.com/qps/rest/3.0/count/was/webapp' ] >>> xml_output = subprocess.check_output(args).decode('utf-8') % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 138 276 0 276 0 0 190 0 --:--:-- 0:00:01 --:--:-- 315 >>> xml_output u'<?xml version="1.0" encoding="UTF-8"?>

POST binary data using httplib cause Unicode exceptions

微笑、不失礼 提交于 2019-12-02 03:15:59
问题 When i try to send an image with urllib2 the UnicodeDecodeError exception is occured. HTTP Post body: f = open(imagepath, "rb") binary = f.read() mimetype, devnull = mimetypes.guess_type(urllib.pathname2url(imagepath)) body = """Content-Length: {size} Content-Type: {mimetype} {binary} """.format(size=os.path.getsize(imagepath), mimetype=mimetype, binary=binary) request = urllib2.Request(url, body, headers) opener = urllib2.build_opener(urllib2.HTTPSHandler(debuglevel=1)) response = opener

urllib2 HTTPPasswordMgr not working - Credentials not sent error

淺唱寂寞╮ 提交于 2019-12-01 23:34:35
The following python curl call has the following successful results: >>> import subprocess >>> args = [ 'curl', '-H', 'X-Requested-With: Demo', 'https://username:password@qualysapi.qualys.com/qps/rest/3.0/count/was/webapp' ] >>> xml_output = subprocess.check_output(args).decode('utf-8') % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 138 276 0 276 0 0 190 0 --:--:-- 0:00:01 --:--:-- 315 >>> xml_output u'<?xml version="1.0" encoding="UTF-8"?>\n<ServiceResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation=

How do I make a PATCH request in Python?

假如想象 提交于 2019-12-01 15:44:42
Is there a way to make a request using the PATCH HTTP method in Python? I tried using httplib, but it doesn't accept PATCH as method param. Kenneth Reitz With Requests , making PATCH requests is very simple: import requests r = requests.patch('http://httpbin.org/patch') Travis Jensen Seems to work in 2.7.1 as well. >>> import urllib2 >>> request = urllib2.Request('http://google.com') >>> request.get_method = lambda: 'PATCH' >>> resp = urllib2.urlopen(request) Traceback (most recent call last): ... urllib2.HTTPError: HTTP Error 405: Method Not Allowed I tried this in Python 3, and it seemed to

Python not getting IP if cable connected after script has started

冷暖自知 提交于 2019-12-01 11:52:35
问题 I hope this doesn't cross into superuser territory. So I have an embedded linux, where system processes are naturally quite stripped. I'm not quite sure which system process monitors to physical layer and starts a dhcp client when network cable is plugged in, but i made one myself. ¨ The problem is, that if i have a python script, using http connections, running before i have an IP address, it will never get a connection. Even after i have a valid IP, the python still has "Temporary error in