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是內網IP。使用IE,火狐什麼的瀏覽器存取檔案都是能下載的。就是用requests存取報錯500,真心不知道為什麼,求解!
下面是程式碼:
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)
以下是瀏覽器發送得到的訊息:
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
以下是網頁回傳的訊息:
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
原來是header的問題,'Referer'加上就好了
全段程式碼貼上來才好分析,現在看來你需要知道瀏覽器下載文件那個時刻的所有信息,才能夠用python模擬,要么你可以直接在伺服器端找到500的原因也可以。
下載檔案需要用r.content, 另外post登入成功之後cookie是已經在s裡面了,不需要在get的時候用參數傳遞