Python 3 urllib POST submit

I would like to write a Python script to automatically log into my broadband counter account. I have never sent a POST before, and I have problems with this.

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 HTML, I got that IDToken1 is the username identifier, and IDToken2 is the password identifier.

Here is my problem:

  • When you enter the correct username and password, the login page is loaded, but:

  • When I enter the wrong username or password, I get a page with the message:

    This server has detected an internal error that is preventing it from completing your request. The most likely cause is an incorrect configuration. Ask the administrator to find messages in the server error log.

+6
source share
2 answers
 details = urllib.parse.urlencode({'IDToken1': 'USERNAME', 'IDToken2': 'PASSWORD'}) 

Add the following line:

 details = details.encode('UTF-8') 
+7
source

It may be by design. What happens if you do this in a browser? The fact that it works with the correct data means that you are doing it right.

0
source

Source: https://habr.com/ru/post/887010/


All Articles