Rectangle 27 112

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 = urllib2.urlopen(request).read()

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

python header urllib2
Rectangle 27 1

import StringIO, httplib

ucode_data = unicode( your_raw_data ,"utf-8")
str = StringIO.StringIO( ucode_data )
http_header = httplib.HTTPMessage(str,0)
http_header.readheaders()

print http_header.__dict__

but it does not decode the request (eg, GET /index.html HTTP/1.2) - it will decode the rest for you though

how to decode HTTP request headers and body in Python 3? - Stack Overf...

python http http-headers python-3.x
Rectangle 27 11

import urllib2

request_headers = {
"Accept-Language": "en-US,en;q=0.5",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Referer": "http://thewebsite.com",
"Connection": "keep-alive" 
}

request = urllib2.Request("http://thewebsite.com", headers=request_headers)
contents = urllib2.urlopen(request).read()
print contents

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

python header urllib2
Rectangle 27 102

urllib2 can be used to perform a HEAD request. This is a little nicer than using httplib since urllib2 parses the URL for you instead of requiring you to split the URL into host name and path.

>>> import urllib2
>>> class HeadRequest(urllib2.Request):
...     def get_method(self):
...         return "HEAD"
... 
>>> response = urllib2.urlopen(HeadRequest("http://google.com/index.html"))

Headers are available via response.info() as before. Interestingly, you can find the URL that you were redirected to:

>>> print response.geturl()
http://www.google.com.au/index.html

response.info().__str__() will return string format of the header, in case you want to do something with the result you get.

except that trying this with python 2.7.1 (ubuntu natty), if there's a redirect, it does a GET on the destination, not a HEAD...

That's the advantage of the httplib.HTTPConnection, which doesn't handle redirects automatically.

but with doshea's answer. how to set the timeout? How to handle bad URLs, i.e., URLs that are not no longer alive.

How do you send a HEAD HTTP request in Python 2? - Stack Overflow

python python-2.7 http http-headers content-type
Rectangle 27 102

urllib2 can be used to perform a HEAD request. This is a little nicer than using httplib since urllib2 parses the URL for you instead of requiring you to split the URL into host name and path.

>>> import urllib2
>>> class HeadRequest(urllib2.Request):
...     def get_method(self):
...         return "HEAD"
... 
>>> response = urllib2.urlopen(HeadRequest("http://google.com/index.html"))

Headers are available via response.info() as before. Interestingly, you can find the URL that you were redirected to:

>>> print response.geturl()
http://www.google.com.au/index.html

response.info().__str__() will return string format of the header, in case you want to do something with the result you get.

except that trying this with python 2.7.1 (ubuntu natty), if there's a redirect, it does a GET on the destination, not a HEAD...

That's the advantage of the httplib.HTTPConnection, which doesn't handle redirects automatically.

but with doshea's answer. how to set the timeout? How to handle bad URLs, i.e., URLs that are not no longer alive.

How do you send a HEAD HTTP request in Python 2? - Stack Overflow

python python-2.7 http http-headers content-type
Rectangle 27 102

urllib2 can be used to perform a HEAD request. This is a little nicer than using httplib since urllib2 parses the URL for you instead of requiring you to split the URL into host name and path.

>>> import urllib2
>>> class HeadRequest(urllib2.Request):
...     def get_method(self):
...         return "HEAD"
... 
>>> response = urllib2.urlopen(HeadRequest("http://google.com/index.html"))

Headers are available via response.info() as before. Interestingly, you can find the URL that you were redirected to:

>>> print response.geturl()
http://www.google.com.au/index.html

response.info().__str__() will return string format of the header, in case you want to do something with the result you get.

except that trying this with python 2.7.1 (ubuntu natty), if there's a redirect, it does a GET on the destination, not a HEAD...

That's the advantage of the httplib.HTTPConnection, which doesn't handle redirects automatically.

but with doshea's answer. how to set the timeout? How to handle bad URLs, i.e., URLs that are not no longer alive.

How do you send a HEAD HTTP request in Python 2? - Stack Overflow

python python-2.7 http http-headers content-type
Rectangle 27 102

urllib2 can be used to perform a HEAD request. This is a little nicer than using httplib since urllib2 parses the URL for you instead of requiring you to split the URL into host name and path.

>>> import urllib2
>>> class HeadRequest(urllib2.Request):
...     def get_method(self):
...         return "HEAD"
... 
>>> response = urllib2.urlopen(HeadRequest("http://google.com/index.html"))

Headers are available via response.info() as before. Interestingly, you can find the URL that you were redirected to:

>>> print response.geturl()
http://www.google.com.au/index.html

response.info().__str__() will return string format of the header, in case you want to do something with the result you get.

except that trying this with python 2.7.1 (ubuntu natty), if there's a redirect, it does a GET on the destination, not a HEAD...

That's the advantage of the httplib.HTTPConnection, which doesn't handle redirects automatically.

but with doshea's answer. how to set the timeout? How to handle bad URLs, i.e., URLs that are not no longer alive.

How do you send a HEAD HTTP request in Python 2? - Stack Overflow

python python-2.7 http http-headers content-type
Rectangle 27 98

edit: This answer works, but nowadays you should just use the requests library as mentioned by other answers below.

>>> import httplib
>>> conn = httplib.HTTPConnection("www.google.com")
>>> conn.request("HEAD", "/index.html")
>>> res = conn.getresponse()
>>> print res.status, res.reason
200 OK
>>> print res.getheaders()
[('content-length', '0'), ('expires', '-1'), ('server', 'gws'), ('cache-control', 'private, max-age=0'), ('date', 'Sat, 20 Sep 2008 06:43:36 GMT'), ('content-type', 'text/html; charset=ISO-8859-1')]

There's also a getheader(name) to get a specific header.

this response marked as answered but one should look at the requests lib. Look at the Dalius's response that is a bit below.

This is really nice, but it requires you to have separate values for the host and path of the request. It's useful to have urlparse at hand, which is shown by some lower-ranked reponse.

httplib
http.client

Unfortunately, requests isn't shipped with Python by default.

How do you send a HEAD HTTP request in Python 2? - Stack Overflow

python python-2.7 http http-headers content-type
Rectangle 27 98

edit: This answer works, but nowadays you should just use the requests library as mentioned by other answers below.

>>> import httplib
>>> conn = httplib.HTTPConnection("www.google.com")
>>> conn.request("HEAD", "/index.html")
>>> res = conn.getresponse()
>>> print res.status, res.reason
200 OK
>>> print res.getheaders()
[('content-length', '0'), ('expires', '-1'), ('server', 'gws'), ('cache-control', 'private, max-age=0'), ('date', 'Sat, 20 Sep 2008 06:43:36 GMT'), ('content-type', 'text/html; charset=ISO-8859-1')]

There's also a getheader(name) to get a specific header.

this response marked as answered but one should look at the requests lib. Look at the Dalius's response that is a bit below.

This is really nice, but it requires you to have separate values for the host and path of the request. It's useful to have urlparse at hand, which is shown by some lower-ranked reponse.

httplib
http.client

Unfortunately, requests isn't shipped with Python by default.

How do you send a HEAD HTTP request in Python 2? - Stack Overflow

python python-2.7 http http-headers content-type
Rectangle 27 98

edit: This answer works, but nowadays you should just use the requests library as mentioned by other answers below.

>>> import httplib
>>> conn = httplib.HTTPConnection("www.google.com")
>>> conn.request("HEAD", "/index.html")
>>> res = conn.getresponse()
>>> print res.status, res.reason
200 OK
>>> print res.getheaders()
[('content-length', '0'), ('expires', '-1'), ('server', 'gws'), ('cache-control', 'private, max-age=0'), ('date', 'Sat, 20 Sep 2008 06:43:36 GMT'), ('content-type', 'text/html; charset=ISO-8859-1')]

There's also a getheader(name) to get a specific header.

this response marked as answered but one should look at the requests lib. Look at the Dalius's response that is a bit below.

This is really nice, but it requires you to have separate values for the host and path of the request. It's useful to have urlparse at hand, which is shown by some lower-ranked reponse.

httplib
http.client

Unfortunately, requests isn't shipped with Python by default.

How do you send a HEAD HTTP request in Python 2? - Stack Overflow

python python-2.7 http http-headers content-type
Rectangle 27 98

edit: This answer works, but nowadays you should just use the requests library as mentioned by other answers below.

>>> import httplib
>>> conn = httplib.HTTPConnection("www.google.com")
>>> conn.request("HEAD", "/index.html")
>>> res = conn.getresponse()
>>> print res.status, res.reason
200 OK
>>> print res.getheaders()
[('content-length', '0'), ('expires', '-1'), ('server', 'gws'), ('cache-control', 'private, max-age=0'), ('date', 'Sat, 20 Sep 2008 06:43:36 GMT'), ('content-type', 'text/html; charset=ISO-8859-1')]

There's also a getheader(name) to get a specific header.

this response marked as answered but one should look at the requests lib. Look at the Dalius's response that is a bit below.

This is really nice, but it requires you to have separate values for the host and path of the request. It's useful to have urlparse at hand, which is shown by some lower-ranked reponse.

httplib
http.client

Unfortunately, requests isn't shipped with Python by default.

How do you send a HEAD HTTP request in Python 2? - Stack Overflow

python python-2.7 http http-headers content-type
Rectangle 27 49

Requests
import requests

resp = requests.head("http://www.google.com")
print resp.status_code, resp.text, resp.headers

How do you send a HEAD HTTP request in Python 2? - Stack Overflow

python python-2.7 http http-headers content-type
Rectangle 27 49

Requests
import requests

resp = requests.head("http://www.google.com")
print resp.status_code, resp.text, resp.headers

How do you send a HEAD HTTP request in Python 2? - Stack Overflow

python python-2.7 http http-headers content-type
Rectangle 27 49

Requests
import requests

resp = requests.head("http://www.google.com")
print resp.status_code, resp.text, resp.headers

How do you send a HEAD HTTP request in Python 2? - Stack Overflow

python python-2.7 http http-headers content-type
Rectangle 27 48

Requests
import requests

resp = requests.head("http://www.google.com")
print resp.status_code, resp.text, resp.headers

How do you send a HEAD HTTP request in Python 2? - Stack Overflow

python python-2.7 http http-headers content-type
Rectangle 27 34

I believe the Requests library should be mentioned as well.

I agree It was very simple to make requests: {code} import requests r = requests.head('github.com') {code}

@LuisR.: if there is a redirect then it follows GET/POST/PUT/DELETE also.

@Nick Retallack: there is no easy way to disable redirects. allow_redirects can disable only POST/PUT/DELETE redirects. Example: head request no redirect

@J.F.Sebastian The link to your example seems to be broken. Could you elaborate on the issue with following redirects?

How do you send a HEAD HTTP request in Python 2? - Stack Overflow

python python-2.7 http http-headers content-type
Rectangle 27 34

I believe the Requests library should be mentioned as well.

I agree It was very simple to make requests: {code} import requests r = requests.head('github.com') {code}

@LuisR.: if there is a redirect then it follows GET/POST/PUT/DELETE also.

@Nick Retallack: there is no easy way to disable redirects. allow_redirects can disable only POST/PUT/DELETE redirects. Example: head request no redirect

@J.F.Sebastian The link to your example seems to be broken. Could you elaborate on the issue with following redirects?

How do you send a HEAD HTTP request in Python 2? - Stack Overflow

python python-2.7 http http-headers content-type
Rectangle 27 34

I believe the Requests library should be mentioned as well.

I agree It was very simple to make requests: {code} import requests r = requests.head('github.com') {code}

@LuisR.: if there is a redirect then it follows GET/POST/PUT/DELETE also.

@Nick Retallack: there is no easy way to disable redirects. allow_redirects can disable only POST/PUT/DELETE redirects. Example: head request no redirect

@J.F.Sebastian The link to your example seems to be broken. Could you elaborate on the issue with following redirects?

How do you send a HEAD HTTP request in Python 2? - Stack Overflow

python python-2.7 http http-headers content-type
Rectangle 27 34

I believe the Requests library should be mentioned as well.

I agree It was very simple to make requests: {code} import requests r = requests.head('github.com') {code}

@LuisR.: if there is a redirect then it follows GET/POST/PUT/DELETE also.

@Nick Retallack: there is no easy way to disable redirects. allow_redirects can disable only POST/PUT/DELETE redirects. Example: head request no redirect

@J.F.Sebastian The link to your example seems to be broken. Could you elaborate on the issue with following redirects?

How do you send a HEAD HTTP request in Python 2? - Stack Overflow

python python-2.7 http http-headers content-type
Rectangle 27 57

import urllib2
request = urllib2.Request('http://localhost:8080')
request.get_method = lambda : 'HEAD'

response = urllib2.urlopen(request)
print response.info()

Tested with quick and dirty HTTPd hacked in python:

Server: BaseHTTP/0.3 Python/2.6.6
Date: Sun, 12 Dec 2010 11:52:33 GMT
Content-type: text/html
X-REQUEST_METHOD: HEAD
Sun Dec 12 12:52:28 2010 Server Starts - localhost:8080
localhost.localdomain - - [12/Dec/2010 12:52:33] "HEAD / HTTP/1.1" 200 -
import httplib2
h = httplib2.Http()
resp = h.request("http://www.google.com", 'HEAD')

Making HTTP HEAD request with urllib2 from Python 2 - Stack Overflow

python python-2.7 urllib2 head