User
User

Reputation: 13

Python HTTP HEAD request keepalive

Using Python httplib or httpclient, what code do I need to use in my HTTP client to:

to extend its HTTP connection using Keepalive messages?

I used the following code example but it has two problems:

  1. It does not extend the http connection using Keepalive,
  2. It gives me an error message "500 Domain Not Found" if I use the IP address instead of the domain name.

 

import http.client
Connection = http.client.HTTPConnection("www.python.org")
Connection.request("HEAD", "")
response = Connection.getresponse()
print(response.status, response.reason)

Upvotes: 0

Views: 999

Answers (1)

Juan Fco. Roco
Juan Fco. Roco

Reputation: 1638

requests allows to:

  • send requests with HEAD method:

    import requests
    resp = requests.head("http://www.python.org")
    
  • use sessions for auto Keep-alive: info

    s = requests.Session()
    resp = s.head("http://www.python.org")
    resp2 = s.get("http://www.python.org/")
    
  • Regarding using the IP address instead of domain, that has nothing to do with your request. Most sites use some kind of virtual hosts, so they don't respond to IP address only to specific domain names. If you ask for the IP address you may get a 500 error or a message error.

Upvotes: 2

Related Questions