Python 3, urllib POST submit
Asked Answered
D

2

7

I'd like to write a Python script to auto login to my broadband usage meter account. I've never done a POST submit before and I'm having some trouble with it.

import urllib.request, urllib.parse, urllib.error
import socket

try:
    details = urllib.parse.urlencode({ 'IDToken1': 'USERNAME', 'IDToken2': 'PASSWORD' })
    url = urllib.request.Request('https://login1.telecom.co.nz/distauth/UI/Login?realm=XtraUsers&goto=https%3A%2F%2Fwww.telecom.co.nz%3A443%2Fjetstreamum%2FxtraSum%3Flink%3Drdt', details)
    url.add_header("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.29 Safari/525.13")

    responseData = urllib.request.urlopen(url).read().decode('utf8', 'ignore')
    responseFail = False

except urllib.error.HTTPError as e:
    responseData = e.read().decode('utf8', 'ignore')
    responseFail = False

except urllib.error.URLError:
    responseFail = True

except socket.error:
    responseFail = True

except socket.timeout:
    responseFail = True

except UnicodeEncodeError:
    print("[x]  Encoding Error")
    responseFail = True

print(responseData)

From the HTML I derived that IDToken1 is the username id and IDToken2 is the password id.

Here is my problem:

  • When I enter the correct username and password, the login page loads, but:

  • When I enter the incorrect username or password, I get a page that says:

    This server has encountered an internal error which prevents it from fulfilling your request. The most likely cause is a misconfiguration. Please ask the administrator to look for messages in the server's error log.

Dewayne answered 30/4, 2011 at 13:38 Comment(1)
Well, this is very difficult without seeing the actual page you are using... maybe you should check what is actually sent by your browser with the Chrome dev tools or a packet sniffer like Wireshark?Rachele
I
8
details = urllib.parse.urlencode({'IDToken1': 'USERNAME', 'IDToken2': 'PASSWORD'})

Add the following line:

details = details.encode('UTF-8')
Ivanna answered 8/4, 2013 at 2:39 Comment(0)
C
0

That could be by design. What happens if you do it in a browser? The fact that it works with correct data means that you're doing it right.

Cassiecassil answered 6/2, 2013 at 14:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.