Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3, urllib POST submit

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.

like image 771
Rhys Avatar asked Apr 30 '11 13:04

Rhys


People also ask

How do I request a post using Urllib?

To make a basic request in Python 3, you will need to import the urllib. request module, this contains the function urlopen() which you can use to make a request to a specified URL. To fetch the actual output of the request, you can use the read() function on the returned object to read the contents.

How do you send data to a URL in Python?

The post() method sends a POST request to the specified url. The post() method is used when you want to send some data to the server.

Is Urllib built in Python 3?

The urllib module in Python 3 allows you access websites via your program. This opens up as many doors for your programs as the internet opens up for you. urllib in Python 3 is slightly different than urllib2 in Python 2, but they are mostly the same.


1 Answers

details = urllib.parse.urlencode({'IDToken1': 'USERNAME', 'IDToken2': 'PASSWORD'})

Add the following line:

details = details.encode('UTF-8')
like image 133
Chris Avatar answered Nov 14 '22 22:11

Chris