Laszlo Zsolt Nagy wrote:
> 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("http://xop-pc.main.com")
>> htmlSource = sock.read()
>> sock.close() print
>> htmlSource
>> In the above code how can i supply username and password to that URL.
>> Thanks for you time.
>>
>>
> xop-pc.main.com 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:
>
> http://docs.python.org/lib/urlopener-objs.html
>
> 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 = opener.open("http://xop-pc.main.com")
> try:
> html_source = f.read()
> finally:
> f.close()
>
>
>
> Best,
>
> Les
>
>
I tried to view the source,it has,
....
....
src="/em/cabo/images /t.gif" height="80"></td><td><table
align="center" border="0" cellspacing="2" cellpadding="0"><tr id="username
__xc_"><td align="right" nowrap><span class="x8"><span
title="Required" class="xc">*</span> *User Name</s
pan></td><td width="12"><img src="/em/cabo/images/t.gif"
width="12"></td><td valign="top" nowrap><input
id="username" class="x4" onkeypress="return
_submitOnEnter(event, 'User');" name="j_username" size="30"
type="text" value="myadmin"></td></tr><tr><td align="right"
nowrap><span class="x8"><span title="Required"
class="xc">*</span> Password</span></td><td width="12"><img
src="/em/cabo/images/t.gif" width="12"></td
><td valign="top" nowrap><input id="M__Id" class="x4"
name="j_password" size="30*" autocomplete="off" type="p
...
...
Thanks
Amen
*
*
>
>
>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-2" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
<br>
Laszlo Zsolt Nagy wrote:
<blockquote cite="midmailman.1594.1128589260.509.python-list@python.org"
type="cite">Murugesh wrote:
<br>
<br>
<blockquote type="cite">Hi all,
<br>
I'm a newbie to python.I need to login to a webpage after supplying
usename and password.
<br>
<br>
import urllib sock = urllib.urlopen(<a class="moz-txt-link-rfc2396E" href="http://xop-pc.main.com">"http://xop-pc.main.com"</a>)
<br>
htmlSource = sock.read()
sock.close() print
htmlSource <br>
In the above code how can i supply username and password to that URL.
<br>
Thanks for you time.
<br>
<br>
<br>
</blockquote>
xop-pc.main.com is not an existing site.
<br>
Can you tell me what kind of authentication method it is using?
<br>
If that is the "basic authentication" (defined the standard HTTP
protocol) then you need to read this:
<br>
<br>
<a class="moz-txt-link-freetext" href="http://docs.python.org/lib/urlopener-objs.html">http://docs.python.org/lib/urlopener-objs.html</a>
<br>
<br>
Basically, you need to subclass URLopener or FancyURLopener, and
overwrite its "prompt_user_passwd" method.
<br>
That method will be then called whenever the server needs
authentication.
<br>
<br>
Here is a template for you (untested):
<br>
<br>
<br>
from urllib import FancyURLOpener
<br>
class MyOpener(FancyURLOpener):
<br>
def prompt_user_passwd(host,realm):
<br>
return ('myusername','mypassword')
<br>
<br>
opener = MyOpener({})
<br>
f = opener.open(<a class="moz-txt-link-rfc2396E" href="http://xop-pc.main.com">"http://xop-pc.main.com"</a>)
<br>
try:
<br>
html_source = f.read()
<br>
finally:
<br>
f.close()
<br>
<br>
<br>
<br>
Best,
<br>
<br>
Les
<br>
<br>
<br>
</blockquote>
<br>
I tried to view the source,it has,<br>
....<br>
....<br>
src="/em/cabo/images /t.gif"
height="80"></td><td><table align="center" border="0"
cellspacing="2" cellpadding="0"><tr id="username<br>
__xc_"><td align="right" nowrap><span
class="x8"><span title="Required"
class="xc">*</span>&nbsp;<b>User Name</s<br>
pan></td><td width="12"><img
src="/em/cabo/images/t.gif" width="12"></td><td
valign="top" nowrap><input<br>
id="username" class="x4" onkeypress="return
_submitOnEnter(event, 'User');" name="j_username" size="30"<br>
type="text"
value="myadmin"></td></tr><tr><td align="right"
nowrap><span class="x8"><span title="Required"<br>
class="xc">*</span>&nbsp;Password</span></td><td
width="12"><img src="/em/cabo/images/t.gif" width="12"></td<br>
><td valign="top" nowrap><input id="M__Id"
class="x4" name="j_password" size="30</b>" autocomplete="off" type="p<br>
...<br>
...<br>
<br>
Thanks<br>
Amen<br>
<b><br>
</b>
<blockquote cite="midmailman.1594.1128589260.509.python-list@python.org"
type="cite"><br>
<br>
<br>
</blockquote>
<br>
</body>
</html>
Received on Sat Oct 15 04:12:46 2005