我试图编写一个脚本,通过代理下载一个文件,并以Minecraft的可执行文件为例。代理似乎不起作用
我尝试过更改代理IP地址,但在使用urllib.request.getproxies()
列出代理时,什么也没有出现
以下是我为解决此问题而查看的一些链接,包括StackOverflow和官方文档上的帖子:
urllib.request.urlretrieve with proxy?
Python urllib urlretrieve behind proxy
https://docs.python.org/3.5/library/urllib.request.html#urllib.request.ProxyHandler
https://www.programcreek.com/python/example/75591/urllib.request.ProxyHandler
https://docs.python.org/3/howto/urllib2.html
理想情况下,我希望继续使用这个库,因为有些计算机没有安装像requests
这样的库,我希望它在任何地方都可以开箱即用,而不需要下载额外的东西
编辑:我的代码非常仓促,写得很糟糕,对此我深表歉意。整个项目比任何其他项目都更能证明概念
import urllib.request
import sys
import os
import subprocess
# URL with port for proxy server (find one at hidester.com)
proxyurl = "IP ADDRESS HERE - I have tried several"
useproxy = True
# Set constants for easy editing later
downloadurl = "https://launcher.mojang.com/download/"
downloadfilename = "Minecraft.exe"
launcherfilename = "launch_minecraft.bat"
downloadpath = os.path.join("bin", downloadfilename)
def main():
print("Setting up...\n")
# Inital setup - create necessary paths
if not os.path.exists("bin"):
os.makedirs("bin")
if not os.path.exists("data"):
os.makedirs("data")
if not os.path.exists(downloadpath):
download()
else:
print("Minecraft has already been downloaded! Download again?")
choice = input("[Y/n]: ")
if choice == "y" or choice == "Y":
try:
os.remove(downloadpath)
except:
exit("ERROR: Could not remove file.")
download()
if not os.path.exists(launcherfilename):
createbatch()
print("==== Setup complete! ====\n")
launchgame()
def download():
if useproxy == True:
# Setup proxy handler
proxy = urllib.request.ProxyHandler({'http': proxyurl})
opener = urllib.request.build_opener(proxy)
urllib.request.install_opener(opener)
if len(urllib.request.getproxies()) < 1:
exit("ERROR: No proxy!")
print('Downloading file from "{0}" using proxy server "{1}"\n'.format(downloadurl, proxyurl))
else:
# Not using proxy
print('Downloading file from "{0}" without proxy\n'.format(downloadurl))
# Download latest EXE
try:
urllib.request.urlretrieve(downloadurl + downloadfilename, downloadpath, reporthook)
print("Download finished!\n")
except:
exit("ERROR: Could not download file.")
def createbatch():
# Create a file to launch the launcher with the correct working directory
# NOTE: I download the file from GitHub here rather than writing directly to a file to avoid the weirdness with Unicode characters in Python.
print("Downloading launcher file...")
if os.name == "nt":
try:
urllib.request.urlretrieve("https://gist.githubusercontent.com/iCrazyBlaze/a8a3fdf89377a901a250f4a36c459415/raw/d66d0e9b7fa06836c33d8254504496a9b65d591a/launch_minecraft.bat", launcherfilename)
print("Download finished!\n")
except:
print("ERROR: Could not download file.")
def launchgame():
if os.name == "nt":
subprocess.call(launcherfilename)
# Display progress on screen
def reporthook(blocknum, blocksize, totalsize):
readsofar = blocknum * blocksize
if totalsize > 0:
percent = readsofar * 1e2 / totalsize
s = "\r%5.1f%% %*d / %d" % (
percent, len(str(totalsize)), readsofar, totalsize)
sys.stderr.write(s)
if readsofar >= totalsize: # near the end
sys.stderr.write("\n")
else: # total size is unknown
sys.stderr.write("Read %d\n" % (readsofar,))
if __name__ == "__main__":
main()
目前没有回答
相关问题 更多 >
编程相关推荐