Why does this python script not log into my website?

Using python 3 in 2008R2:   I had some help getting this far but I do not understand why it does not login to my website. If I print to a file the file is blank. If I print to screen I get the page where you would enter the login information. Can someone help?

#We need urllib module
import urllib.parse
import urllib.request
#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
params2 = urllib.parse.urlencode({'userName': 'UID', 'Password': 'password', 'submit' : 'login'})
#Encode the UID and password in ascii
params = params2.encode('ascii')
#This is the url of the login page, put the real url here
site = "https://mysite.mycompany.com"
#We save the result of the post in variable f
f = urllib.request.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
result = open('c:\\phython.html',w)
#Finished, so we close f
f.close()
LVL 1
jimmylew52Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jimmylew52Author Commented:
I think the UserName and Password entry are correct:

"UserName" value=\'\'  style="width:180px" required/>\r\n                    </t
d>\r\n                 </tr>\r\n                 <tr>\r\n                    <td
>\r\n                       Password:\r\n                    </td>\r\n
           <td>\r\n                       <input type="password"  id="txtPassword" name="Password" value=\'\' style="width:180px" required/>\r\n
    </td>\r\n                 </tr>\r\n                  <tr>          \r\n
                <td colspan="2">\r\n                      <input type="button" i
d="btnLogin" style="font-size:11px" class="k-button"  value="Login" /> \r\n\r\n
                   </td>\r\n                 </tr>\r\n                 <tr>\r\n
                 <td colspan="2">\r\n                         <label id="lblErro
0
Walter RitzelSenior Software EngineerCommented:
Jimmy,
I think the problem may be your URL. Are you sure that the code behind the page on URL  "https://mysite.mycompany.com" is prepared to receive the fields your are sending and process the login? Maybe the URL to process the login is a different one (I would say for you to check the complete login form code to see what is the action).
I mean: It seems your form is expecting fields txtUserName and txtPassword, but your are sending userName and Password... I would try to change that. Also, please put the same value on ID and NAME of your form fields.
0
jimmylew52Author Commented:
Here is what is there:

     UserName: $("#txtUserName").va
l(),\r\n                        Password: $("#txtPassword").val()\r\n

  $(\'#btnLogin\').focus().click();\r\n

These values work in an obsolete OS for powershell, they do not work at all running in 2008R2:

 $usernameField = $doc.getElementById('userName')
      #write-host $usernameField
    $usernameField.value = $username
      #write-host $username
    # Find the password field and set the value to that of the result
    $passwordField = $doc.getElementById('password')
      $passwordField.value = $pass
      #write-host $pass
    # Find and click the submit button
    $submitButton = $doc.getElementById('login')
      #write-host $submitButton
    $submitButton.click()
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

jimmylew52Author Commented:
I tried  params2 = urllib.parse.urlencode({'txtUserName': 'UID', 'txtPassword': 'password', 'submit' : 'Login'})

Still no login
0
Walter RitzelSenior Software EngineerCommented:
Can you please describe your full problem? I mean, the architecture you have, what are you trying to accomplish, etc... I'm confused with the powershell you mentioned...
0
jimmylew52Author Commented:
I am running a powershell script on window 2003 server that checks the login capability of applications that are running on 2008R2. This powershell script works most of the time when it is run on a 2003 server. If I try to run the powershell script on a 2008R2 server it does not work at all. It says none of the servers checked can be logged into.

I nee something that will run on server 2008R2 that will check the login capabilities of the applications.
Question about powershell ask on this forum go unanswered, i assume because no one know shat the problem might be. The last response I have had suggested the Python script.

I cannot get the python script to log into the servers.
0
Walter RitzelSenior Software EngineerCommented:
Ok, got it. So, here are my suggestion:
1) for each application, you should identify the way the application does process the login. For example, if the application uses single sign on, your current script will not be able to validate;
2) the correct page for login. In your example, you have used just the general address of the application. You should identify correctly the page to process the login. For example, if the page that is the action of the login form is called processlogin.aspx, use that as the address;

The rest is what I have written before: make sure the variables are correct and make adjustments if needed.

Please check those points and let us know.
0
jimmylew52Author Commented:
1) neither application uses single sign on, checked with the developers

2)The URL I am using is the correct page for login, checked with the developers

The variables are correct, again, checked with the developers

It is odd the Powershell script will log in and works on 2003R2 server but not on 2008R2.

The python script does not login on 2008R2, I have not tried it on 2003R2 but the powershell script, I'm using the same variables will log in from both 2003R2 and 2008R2 but the script fails to test for the login on 2008R2.
0
Walter RitzelSenior Software EngineerCommented:
Well, the last thing you can do is to debug the situation. Use the developer tools from Chrome to see how the headers of the login page are composed when you access the application normally and see what python generates and try to make sure that python code will generate the same thing.
0
jimmylew52Author Commented:
see how the headers of the login page are composed -  What am I looking for?

I don't use chrome, will fire foxes firebug do the same thing?

see what python generates and try to make sure that python code will generate the same thing - I wouldn't know how to start doing this. How would I use Chrome to see what Python is doing?
0
Walter RitzelSenior Software EngineerCommented:
Yes, I think you can do that with Firebug. In Chrome, you can install Postman and, when you use it, you type the request you are doing, and you will see all HTTP headers. In fact, you can simulate your python code and see what is happening.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jimmylew52Author Commented:
I will give that a try
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.