By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,124 Members | 1,566 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,124 IT Pros & Developers. It's quick & easy.

Help-log in to a web page

P: n/a
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.

Thanks
Amen.

Oct 6 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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


Oct 6 '05 #2

P: n/a

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>&nbsp;*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>&nbsp;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
*
*


Oct 6 '05 #3

P: n/a
Murugesh wrote:

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>&nbsp;*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>&nbsp;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
*
*

Les,
I tried your code and it hangs.I believe the authentication is different
from the standard one.

Any help will be appreciated.

Thanks
Amen

Oct 6 '05 #4

P: n/a
On Thu, 06 Oct 2005 14:51:31 +0530, Murugesh wrote:
<input
id="username" class="x4" onkeypress="return
_submitOnEnter(event, 'User');" name="j_username" size="30"
type="text" value="myadmin">


I'm a novice too. But that looks to me like a javascript method. At a
guess, it probably re directs the browser to some web script which checks
the password.

If so, knowing the address of that script, and the names of the keys it's
expecting would be useful to you. They might be in the <head></head>
section of the page. If not, they might be contained in a *.js file
somewhere.

I have no idea whether this helpful at all. Just consider it speculation.
--
www.wintergreen.in

Oct 6 '05 #5

P: n/a

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>&nbsp;*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>&nbsp;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
...
...

Either it is a javascript or a form. Most likely this will be a normal
form, like:

<form method="POST" action="login.php">
<input name="login" type="text">
<input name="pwd" type="text">
</form>

This is another authentication method that is used frequently. You need
to POST the login/password and other parameters to the URL specified by
the "action" attribute of the "form" tag.

Say, if you download the above HTML page from http://here.net/there

then you should POST your login data to

http://here.net/there/login.php

The response may contain cookies - you need to use them to keep yourself
logged in.

Please read documentation about these:

- HTML forms
- HTTP cookies

and then read the docs of the urllib module here:

http://www.python.org/doc/current/li...le-urllib.html

It has parts where you can learn how to POST or GET data.
If you need to handle cookies as well (probably, this is true in your
case), you might want to change to urllib2

http://www.python.org/doc/current/li...e-urllib2.html

because it has support for cookies.

Don't do anything until you understand what cookies and HTML forms are.
(And don't ask about them here - it is not about Python programming...)

Best,

Les
Oct 6 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.