简体   繁体   English

下载仅允许使用Python从内部服务器访问的文件

[英]Download files that only allow access from internal server with Python

I was trying to download some files from a sever but it came back with an error page saying only links from internal server is allowed. 我试图从服务器下载一些文件,但返回错误页面,提示仅允许来自内部服务器的链接。 I was able to download the file with any browser by clicking the link and I have verified the link I captured in Python was correct. 通过单击链接,我可以使用任何浏览器下载文件,并且已经验证了我在Python中捕获的链接是正确的。 Is there any way this can be done by using python? 有什么办法可以使用python做到这一点吗? I tried urllib, urllib2 and requests, but none works. 我尝试了urllib,urllib2和请求,但是没有用。 I could use selenium but the solution is not elegent 我可以使用硒,但解决方案不是紧急的

When you use your browser, it sends a header known as a User-Agent that identifies it. 使用浏览器时,它将发送一个称为User-Agent的标头来对其进行标识。 You need to 'spoof' the user agent from your python script to make it think a human is browsing the website. 您需要从python脚本中“欺骗”用户代理,以使其认为有人在浏览网站。 Set the User-Agent header to that of a common browser, this makes it difficult for the server to detect that you are using a script. 将User-Agent标头设置为通用浏览器的标头,这会使服务器很难检测到您正在使用脚本。

This should work if the webserver is checking on the referer (the address that contains the link): 如果网络服务器正在检查引荐来源地址(包含链接的地址),则此方法应该起作用:

import urllib2
req = urllib2.Request('http://www.example.com/linked_resource')
req.add_header('Referer', 'http://www.example.com/page_with_link')
r = urllib2.urlopen(req)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从服务器下载所有 pdf 文件,无需访问主页 - 仅访问文件 - download all pdf's files from server, without access to the main page - only to files 限制仅在服务器上执行Python文件(防止从浏览器访问) - Restrict execution of Python files only to server (prevent access from browser) 如何允许用户访问覆盖服务器 Python Flask 上的文件? - How to allow users access to overwrite files on server Python Flask? 仅从 Python 中的远程 SFTP 服务器下载新文件,忽略文件扩展名 - Download new files only, ignoring file extensions, from remote SFTP server in Python 使用python,如何连接到WebDAV服务器并从其中下载文件? - With python, how can I connect to and download files from, a WebDAV server? 使用 Python 从包含给定字符串的 FTP 服务器下载文件 - Download files from an FTP server containing given string using Python Python从公共FTP服务器下载zip文件 - Python download zip files from a public FTP server 使用Python从SFTP服务器下载超过5天的文件 - Download files from SFTP server that are older than 5 days using Python 循环使用 python 从 SFTP 服务器下载多个文件 - Download multiple files from SFTP server using python in a loop 如何使用 Python 从基于 web 的文件服务器下载文件 - How to download files from a web based file server with Python Mechanize
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM