Help-log in to a web page

Laszlo Zsolt Nagy gandalf at
Thu Oct 6 11:00:54 CEST 2005

Murugesh wrote:

>Hi all,
>I'm a newbie to python.I need to login to a webpage after supplying 
>usename and password.
>import urllib   
>sock = urllib.urlopen("")
>htmlSource =                           
>print htmlSource        
>In the above code how can i supply username and password to that URL.
>Thanks for you time.
> is not an existing site.
Can you tell me what kind of authentication method it is using?
If that is the "basic authentication" (defined the standard HTTP 
protocol) then you need to read this:

Basically, you need to subclass URLopener or FancyURLopener, and 
overwrite its "prompt_user_passwd" method.
That method will be then called whenever the server needs authentication.

Here is a template for you (untested):

from urllib import FancyURLOpener
class MyOpener(FancyURLOpener):
    def prompt_user_passwd(host,realm):
       return ('myusername','mypassword')

opener = MyOpener({})
f ="")
    html_source =



More information about the Python-list mailing list