Python Web Login Script not working

182 views Asked by At

I'm trying to implement a basic Python login script with cookies. I've looked at umpteen examples here and elsewhere and I cannot get past this "urllib2.HTTPError: HTTP Error 405: Method Not Allowed" error.

The script has been copied and crosschecked with a dozen others and I think it makes sense - so perhaps the issue is with the HTML or the webserver? JavaScript is the extent of my web scripting so apologies in advance if the error is embarrassingly obvious.

HTML (index.html):

<form id="loginForm" action="login.py" method="post">
<div class="loginDiv">
    <div class="inputDiv">
        <label for="user" id="user-label">User:</label>
        <input id="username" type="text" name="handle" value=""
               autocomplete="off" class="roundedInputs" style="color:White;width:160px;" />             
    </div>
    <div class="inputDiv">
        <label for="pass" id="pass-label">Pass:</label>
        <input id="password" type=password name="password" value=""
               autocomplete="off" class="roundedInputs"
               style="color:White;width:160px;" />              
    </div>
</div>
<div class="submitDiv">
    <input id="sumbitLogin" class="sumbitStyle" type="submit" value="&#187;&nbsp;ENTER&nbsp;&#171;" />
</div>      
</form>

and Python (login.py)

import cookielib
import urllib
import urllib2

username = 'myuser'
password = 'mypassword'


values = {'username' : username,
          'password' : password }

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_data = urllib.urlencode(values)
opener.open('http://<address>:8080/index.html', login_data)
resp = opener.open('http://<address>:8080/FTP/')
http_headers = response.info()
print resp.read()

I'm on the verge of giving up entirely and writing it in something else so any help would be really appreciated.

1

There are 1 answers

2
James On

I would recommend the Requests library.

import requests
from requests.auth import HTTPBasicAuth

username = 'myuser'
password = 'mypassword'

response = requests.get('http://<address>:8080/index.html', 
                        auth=HTTPBasicAuth(username, password))