s = requests.Session()
r = s.post('http://x.x.x.x/cgi/login.cgi', data={'name': 'ADMIN', 'pwd': 'ADMIN'},verify=False)
r = s.get(url, cookies=s.cookies,stream=True)
print(r.text)
IP is the internal IP. Files can be downloaded using IE, Firefox and other browsers. I just use requests to access and report an error of 500. I really don’t know why. Please help!
The following is the code:
import requests
import ssl
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
ssl._create_default_https_context = ssl._create_unverified_context
s = requests.Session()
requests.packages.urllib3.disable_warnings()
r=s.post('https://172.30.1.141/cgi/login.cgi', data={'name': 'ADMIN', 'pwd': 'ADMIN'},verify=False)
print(r.text)
url='https://172.30.1.141/cgi/url_redirect.cgi?url_name=ikvm&url_type=jwsk'
rr=s.get(url).content
print(rr)
The following is the information sent by the browser:
GET /cgi/url_redirect.cgi?url_name=ikvm&url_type=jwsk HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
X-HttpWatch-RID: 23897-11061
Referer: http://172.30.1.141/cgi/url_redirect.cgi?url_name=man_ikvm
Accept-Language: zh-CN
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
Accept-Encoding: gzip, deflate
Host: 172.30.1.78
Connection: Keep-Alive
Cookie: langSetFlag=0; language=English; SID=ntnfkcvpmqfhmfnt; mainpage=remote; subpage=man_ikvm
The following is the information returned by the web page:
HTTP/1.1 200 OK
Content-Length: 2016
Content-Disposition: inline; filename=launch.jnlp
Content-Type: application/x-java-jnlp-file
Date: Wed, 10 May 2017 21:01:00 GMT
某草草2017-05-18 10:51:32
It turns out to be a header problem, just add 'Referer' and it will be fine
滿天的星座2017-05-18 10:51:32
Post the entire code for analysis. Now it seems that you need to know all the information at the moment when the browser downloads the file before you can simulate it with python, or you can directly find the reason for the 500 error on the server side.
phpcn_u15822017-05-18 10:51:32
You need to use r.content to download the file. In addition, after the post login is successful, the cookie is already in s, so there is no need to pass parameters when getting it
s = requests.Session()
s.post('http://x.x.x.x/cgi/login.cgi', data={'name': 'ADMIN', 'pwd': 'ADMIN'})
r = s.get(url)
with open('文件名', 'wb') as f:
f.write(r.content)