【问题标题】:Download File using python script behind corporate proxy使用公司代理后面的 python 脚本下载文件
【发布时间】:2019-03-29 10:48:27
【问题描述】:

我正在编写一个脚本,该脚本将从网络下载文件....但是有些人坐在公司防火墙上,所以这意味着如果你是@home,下面的代码可以工作,但如果你在办公室,它除非您手动设置代理变量然后运行 ​​....

我在想的是创建一个 if 语句... if 语句将检查用户的 IP 地址,如果他们的用户在 8.x 或 9.x 或 7.x 中有一个 IP 地址,则使用此代理。 .. 否则忽略并继续下载

我用于此下载的代码如下......我对此很陌生,所以我不确定如何为 IP 做一个 if 语句,然后使用代理片,所以任何帮助都会很棒

import urllib.request
import shutil
import subprocess
import os
from os import system

url = "https://downloads.com/App.exe"
output_file = "C:\\User\\Downloads\\App.exe"
with urllib.request.urlopen(url) as response, open(output_file, 'wb') as out_file:
    shutil.copyfileobj(response, out_file)

【问题讨论】:

标签: python python-3.x shell if-statement output


【解决方案1】:

您可以将本地IP读取为@nickthefreak注释,然后使用requests lib建立代理:

import socket
import requests

URL = 'https://downloads.com/App.exe'

if socket.gethostbyname(socket.gethostname()).startswith(('8', '9', '7')):
    r = requests.get(URL, stream=True, proxies={'http': 'http://10.10.1.10:3128', 'https': 'http://10.10.1.10:1080'})
else:
    r = requests.get(URL, stream=True)

with open('C:\\User\\Downloads\\App.exe', 'wb') as f:
    for chunk in r:
        f.write(chunk)

【讨论】:

  • 如何添加代理?
  • 以这种方式下载时gz文件已损坏
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-10
  • 2014-10-28
  • 1970-01-01
  • 2020-02-22
  • 2023-03-25
  • 1970-01-01
  • 2019-10-15
相关资源
最近更新 更多