本文整理汇总了Python中java.net.URL.setFollowRedirects方法的典型用法代码示例。如果您正苦于以下问题:Python URL.setFollowRedirects方法的具体用法?Python URL.setFollowRedirects怎么用?Python URL.setFollowRedirects使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.URL
的用法示例。
在下文中一共展示了URL.setFollowRedirects方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: download_request
# 需要导入模块: from java.net import URL [as 别名]
# 或者: from java.net.URL import setFollowRedirects [as 别名]
def download_request(url, file_path, user_agent, referer_url, timeout, max_redirects, log):
import jarray
from java.net import URL, HttpURLConnection
from java.io import FileOutputStream
try:
input_stream = None
file_output_stream = FileOutputStream(file_path)
HttpURLConnection.setFollowRedirects(True)
first_request = True
is_redirect = False
cookies = None
redirect_counter = 0
while is_redirect or first_request:
http_url_connection = URL(url).openConnection()
http_url_connection.setFollowRedirects(True)
http_url_connection.setInstanceFollowRedirects(True)
http_url_connection.setRequestProperty("Accept-Language", "en-US,en;q=0.8")
http_url_connection.setConnectTimeout(timeout)
http_url_connection.setReadTimeout(timeout)
http_url_connection.setRequestMethod("GET")
http_url_connection.setRequestProperty("User-Agent", user_agent)
http_url_connection.setRequestProperty("Accept-Language", "en-US,en;q=0.8")
if cookies != None and len(cookies) > 0:
http_url_connection.setRequestProperty("Cookie", cookies)
if referer_url != None:
# Note: Referer not Referrer! (see: Wikipedia)
# ^ ^^
http_url_connection.setRequestProperty("Referer", referer_url);
http_url_connection.connect()
first_request = False
# check for redirect
is_redirect = False
status_code = http_url_connection.getResponseCode()
if status_code == HttpURLConnection.HTTP_NOT_FOUND:
raise ScratchtobatHTTP404Error("HTTP 404 NOT FOUND for URL: " + url)
if status_code != HttpURLConnection.HTTP_OK:
if status_code == HttpURLConnection.HTTP_MOVED_TEMP \
or status_code == HttpURLConnection.HTTP_MOVED_PERM \
or status_code == HttpURLConnection.HTTP_SEE_OTHER:
redirect_counter += 1
if redirect_counter > max_redirects:
raise ScratchtobatError("Maximum number of HTTP redirects " \
"{} reached!".format(max_redirects))
is_redirect = True
referer_url = url
# set redirect URL from "location" header field as new URL
url = http_url_connection.getHeaderField("Location")
cookies = http_url_connection.getHeaderField("Set-Cookie")
log.debug("Redirecting to URL: {}".format(url))
input_stream = http_url_connection.getInputStream()
byte_buffer = jarray.zeros(4096, "b")
length = input_stream.read(byte_buffer)
while length > 0:
file_output_stream.write(byte_buffer, 0, length)
length = input_stream.read(byte_buffer)
finally:
try:
if input_stream != None:
input_stream.close()
except:
if file_output_stream != None:
file_output_stream.close()