Home > other >  HTTP Error 400: Bad Request
HTTP Error 400: Bad Request

Time:09-17

I a python novices, just finish see python3 based tutorial, based on the principle of "experiment of truth", trying to write a program "visit family router home page + account login router",
Home router is TP - LINK,

1, the router homepage at http://192.168.1.1

2, the login window only password output, no account input,


3, the python3.7.7 + own IDE


Use requests access no problem,

Using urllib urlopen will go wrong "urllib. Error. HTTPError: HTTP error 400: Bad Request",
 
The import pwdEncrypt # COPY on their network password encryption algorithm
The import requests
The import json
The import urllib

Url="http://192.168.1.1/"

Heads={
"Accept: application/json, text/javascript, */*;
q=0.01,"Accept - the Encoding", "gzip, deflate",
"Accept - Language" : "useful - CN, useful;
q=0.9,"Connection" : "keep - the alive,"
"The Content - Length", "54", # urllib. Error. HTTPError: HTTP error 400: Bad Request
The content-type: "application/json; Charset=utf-8,
""The Host" : "192.168.1.1,"
"Origin" : "http://192.168.1.1/",
"Referer" : "http://192.168.1.1/",
"The user-agent: Mozilla/5.0 (Windows NT 10.0; Win64. X64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36 ",
"X - Requested - With" : "the XMLHttpRequest
"}

The PWD=pwdEncrypt. Encrypt_pwd (' 123456 ')

Datas={
"Method" : "do,"
"Login" : {" password ": the PWD}
}

Def LoginRouterRequests () :
The req=requests. Post (url, json=datas, headers=heads, verify=False)
Print (the req)
Print (the req. Text)

Def LoginRouterUrllib ()
Res=urllib. Request. The request (url, urllib. Parse. Urlencode (datas.) encode (), heads)
The req=urllib. Request. Urlopen (res)
Print (the req)
Print (the req. MSG)
Print (the req. Read (). The decode (' utf-8))

LoginRouterRequests ()
LoginRouterUrllib ()


Manual screen step by step, found that the problem is that "the Content - Length" field in the heads,
Using urlopen, if commented "Content - Length", is not an error, but the router through the verification,

Excuse me, warrior, the problem how to solve???

CodePudding user response:

"Content - Length" is the Content of this sends the message Length, you don't have a Content Length is a fixed value, of course, going to the wrong

CodePudding user response:

The "Content - Length: 54" is my from chrome inside view of the data, then from the browser and input the correct password?
  • Related