开发者

Python 3, urllib POST submit

开发者 https://www.devze.com 2023-03-01 13:43 出处:网络
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.

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 prev开发者_Python百科ents 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.


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

Add the following line:

details = details.encode('UTF-8')


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消