Esqueça urllib e httplib: Requests resolve do jeito certo.
Você pode instalar via pip com:
pip install requests
Depois, veja como é fácil:
>>> import requests
>>> r=requests.get('http://visie.com.br')
>>> for k,v in r.headers.iteritems():print k,'=>',v
...
content-length => 7669
content-encoding => gzip
accept-ranges => bytes
expires => Mon, 20 Jan 2014 13:18:30 GMT
vary => Accept-Encoding,Cookie
server => Apache
last-modified => Mon, 20 Jan 2014 12:38:24 GMT
cache-control => max-age=3, must-revalidate
date => Mon, 20 Jan 2014 13:18:27 GMT
content-type => text/html; charset=UTF-8
>>> r.status_code
200
>>> r.reason
'OK'
>>> r.content[:15]
'<!DOCTYPE html>'
Se você precisar fazer uma requisição HTTPS com autenticação e obter o retorno em JSON:
>>> r=requests.get('https://httpbin.org/basic-auth/user/passwd',auth=('user','passwd'))
>>> r.json()
{u'authenticated': True, u'user': u'user'}
Para fazer POST:
>>> r=requests.post('https://httpbin.org/post',data={'foo':'bar'})
>>> r.json()['form']
{u'foo': u'bar'}
Tudo muito, muito simples. E o módulo faz muito mais e está muito bem documentado. Olhe lá.
Berg Ginú liked this on Facebook.
Carlos André Ferrari liked this on Facebook.
Ótimo para criação de boots (para indexação) ou teste de vulnerabilidades.
Valeu pela dica.
E pro terminal tem o httpie 😉