How do I get the IP address from a http request using the requests library?

It turns out that it’s rather involved. Here’s a monkey-patch while using requests version 1.2.3: Wrapping the _make_request method on HTTPConnectionPool to store the response from socket.getpeername() on the HTTPResponse instance. For me on python 2.7.3, this instance was available on response.raw._original_response. from requests.packages.urllib3.connectionpool import HTTPConnectionPool def _make_request(self,conn,method,url,**kwargs): response = self._old_make_request(conn,method,url,**kwargs) sock = getattr(conn,’sock’,False) if … Read more

Python httplib ResponseNotReady

Previous answers are correct, but there’s another case where you could get that exception: Making multiple requests without reading any intermediate responses completely. For instance: conn.request(‘PUT’,…) conn.request(‘GET’,…) # will not work: raises ResponseNotReady conn.request(‘PUT’,…) r = conn.getresponse() r.read() # <– that’s the important call! conn.request(‘GET’,…) r = conn.getresponse() r.read() # <– same thing and so … Read more

HTTPS connection Python

Python 2.x: docs.python.org/2/library/httplib.html: Note: HTTPS support is only available if the socket module was compiled with SSL support. Python 3.x: docs.python.org/3/library/http.client.html: Note HTTPS support is only available if Python was compiled with SSL support (through the ssl module). #!/usr/bin/env python import httplib c = httplib.HTTPSConnection(“ccc.de”) c.request(“GET”, “https://stackoverflow.com/”) response = c.getresponse() print response.status, response.reason data = … Read more

Python urllib vs httplib?

urllib (particularly urllib2) handles many things by default or has appropriate libs to do so. For example, urllib2 will follow redirects automatically and you can use cookiejar to handle login scripts. These are all things you’d have to code yourself if you were using httplib.

How to send POST request?

If you really want to handle with HTTP using Python, I highly recommend Requests: HTTP for Humans. The POST quickstart adapted to your question is: >>> import requests >>> r = requests.post(“http://bugs.python.org”, data={‘number’: ‘12524’, ‘type’: ‘issue’, ‘action’: ‘show’}) >>> print(r.status_code, r.reason) 200 OK >>> print(r.text[:300] + ‘…’) <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”> … Read more

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