Help-log in to a web page

M

Murugesh

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.
 
L

Laszlo Zsolt Nagy

Murugesh said:
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
 
M

Murugesh

Laszlo said:
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">* said:
><td valign="top" nowrap><input id="M__Id" class="x4"
name="j_password" size="30*" autocomplete="off" type="p
....
....

Thanks
Amen
*
*
 
M

Murugesh

Murugesh said:
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"

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
 
C

Chris Dewin

<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.
 
L

Laszlo Zsolt Nagy

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/lib/module-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/lib/module-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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,771
Messages
2,569,587
Members
45,098
Latest member
KetoBaseReview
Top