Hi,

I am very new to python. Please , Can some one tell me how to post the data to a form. I need to test the user login page by entering username , password values.
There is a also login button next to these fields.

Recommended Answers

All 9 Replies

Hi.
Guess this is what you need:

import urllib

params = urllib.urlencode({'username': 'myuser', 'password': 'mypassword'})
site = "http://www.mysite.com/login.html"
urllib.urlopen(site, params)

Hi.
Guess this is what you need:

import urllib

params = urllib.urlencode({'username': 'myuser', 'password': 'mypassword'})
site = "http://www.mysite.com/login.html"
urllib.urlopen(site, params)

I've tried the code, it seems it is fetching again the same url "http://www.mysite.com/login.html" and in the html code I can see that the user has not logged in.

When I give username and password on the website itself it fetches the page "http://www.mysite.com/index" and in the html code, it clearly mentions that the user has been logged in.


Any ideas??

Um, I realize that I'm not that great with web programming, but don't you first need to know what the variables of the site are for username and password? Then, you just set those variables in python, and pass those to the site's script that logs you in, such as: urllib.urlopen("http://sitehere.com/login.js", params)

You should replace www.mysite.com/login.html with the real url of the login page, and myuser with your real user, same with mypassword, put your real password there. Check in the login page code the exactc field names for user and password.
Try it this way to fetch the result of the login process:

#We need urllib module
import urllib

#These are the user and password parameters, check that field names are the same at the page form and put your real user and password values on myuser and mypassword
params = urllib.urlencode({'user': 'myuser', 'password': 'mypassword'})
#This is the url of the login page, put the real url here
site = "http://www.mysite.com/login.html"
#We save the result of the post in variable f
f = urllib.urlopen(site, params)
#We read the result
result = f.read()
#We print the result, so we can see if we are logged in
print result
#Finished, so we close f
f.close()

I have username, password and Login buttons in html form.
My html code looks like :

           <input class="inputfield" type="text" name="username" tabindex="1" />
                         </div> <!-- formleft --->
          <div id="formright">
                             <label>Password:</label><br />
              <input class="inputfield" type="password" name="password" tabindex="2" />

                         </div>
                        <input class="loginbtn" type="submit" value="Login" tabindex="4" />

and my python code is :

import urllib
params = urllib.urlencode({'username' : 'xxxxx', 'password' : 'xxxxx' , 'submit' : 'Log In'})
top_url = "https://www.mysite.com"
resp = urllib.urlopen(top_url, params)
print resp.read()
print resp.geturl()

When I execute this code, it again fetches the same url 'https://www.mysite.com' instead of logging into the site.

Well, some things here:

params = urllib.urlencode({'username' : 'xxxxx', 'password' : 'xxxxx' , 'submit' : 'Log In'})

You do not need the submit field here, so this line should be:

params = urllib.urlencode({'username' : 'xxxxx', 'password' : 'xxxxx'})

When you log in on a site, it usually redirects to another page, so you catch that url, as seen on your code, but some sites use cookies and other methods to handle sessions, so you must take that in count.
Other thing is that if the site uses secure http (https) it's possible you'll need to provide the keywords key_file and cert_file for client authentication.

Thanks for the clarification.

How do I know which site uses cookies/or not. Does the python code gives any error messsage when try to access the site (that requires cookies) with out using any cookies in the code? .

Well, that's a try / test work with your browser, I think. Understand whow sites handle sessions is a little tricky and don't know if Python says something about, justa get the redirect url. I was gessing you talked about a site made by you, so you had the control of login process.

..Sorry, I didn't get that far (designing a website etc). I had started writing scripts to test the companies websites. :)

Thanks for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.