oriolrius.cat

Des del 2000 compartiendo sobre…

python: Autenticació HTTP de tipus basic-scheme

Reading time: 1 – 2 minutes

Com accedir a un serividor HTTP amb les pàgines protegides amb usuari i password del tipus Basic Authentication Scheme.

import urllib2

theurl = 'http://host:port' username = 'theuser' password = 'thepass'
# this creates a password manager passman = urllib2.HTTPPasswordMgrWithDefaultRealm() # because we have put None at the start it will always use this # username/password combination passman.add_password(None, theurl, username, password) # create the AuthHandler authhandler = urllib2.HTTPBasicAuthHandler(passman) # Return an OpenerDirector instance, which chains the handlers in the # order given Return an OpenerDirector instance, which chains the # handlers in the order given opener = urllib2.build_opener(authhandler) # Install an OpenerDirector instance as the default global opener urllib2.install_opener(opener) # Open the URL url, which can be either a string or a Request object. pagehandle = urllib2.urlopen(theurl) lines = pagehandle.readlines()
for line in lines: print line

L’autentació que usa l’Abyss Webserver per defecte és la del tipus explicada en aquest exemple.

Comments are closed.

Últimas entradas

Resumen 2023

Reading time: 14 – 22 minutes El 2023 comenzó con una sensación de renovación y optimismo. Tras un período marcado por la pandemia, este año se presentó como un lienzo en blanco, lleno de posibilidades y nuevas experiencias. Aunque el

Leer más »
Archivo