Python文件不会下载

2024-04-28 22:59:31 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的代码,所有的URL都在一个配置解析器格式的文件中。当按下按钮时,文件将不会下载。出了什么问题?我用了urllib我应该用urllib2吗?有些函数可能在那里,但没有使用只是忽略了这一点。你知道吗

import wx
import ConfigParser
import urllib

def Download(url):
    response = urllib.urlopen(url).read()
    doned = wx.MessageDialog("Download Done")
    doned.ShowModal()
    doned.Destroy()

#First thing i gona do is Parse 98 box data
BoxParser = ConfigParser.RawConfigParser() #Set Raw
BoxParser.read("98.box") #Mount into 98.box
#Get Vars, and print them

WxTitle = BoxParser.get("meta_data","Window_title") #get the window title
Application = BoxParser.get("meta_data","Application") #Get app name
Desc = BoxParser.get("meta_data","Description") #Get the description of the app
Author = BoxParser.get("meta_data","Author") #Of course! I dont wanna be plagurized
Contact = BoxParser.get("meta_data","Contact_Info") #My Contact Info
Date = BoxParser.get("meta_data","Date") #Date when the current update was made
#UpdateUrl = BoxParser.get("meta_data","Update_url") #Url to update
#BoxUp = BoxParser.get("meta_data","Update_box") #Url to update 98.box
# Meta Data loaded

#time to load the firmwares
e660 = BoxParser.get("Firmware_links","660") #6.60
e6602 = False
e660g = BoxParser.get("Firmware_links","660go") #6.60 Go Eboot
e6602g = False
e639 = BoxParser.get("Firmware_links","639") #6.39
e6392 = False
e639g = BoxParser.get("Firmware_links","639go") #6.39 Go Eboot
e6392g = False
e635 = BoxParser.get("Firmware_links","635") #6.35
e6352 = False
e635g = BoxParser.get("Firmware_links","635go") #6.35 Go Eboot
e6352g = False
e620 = BoxParser.get("Firmware_links","620") #6.20
e550 = BoxParser.get("Firmware_links","550") #5.50
e5502 = False
e500 = BoxParser.get("Firmware_links","500") #5.00
e5002 = False
e401 = BoxParser.get("Firmware_links","401") #4.01
e4012 = False
#Firmwares Loaded








def BoxUpdate():
    Download(Update_box)
    #Check if DD equ true so we can post the MSG
    if downloaddone == True:
        Done2 = wx.MessageDialog(self,"Download Done, 98.Box Updated!")
        Done2.ShowModal()
        Done.Destroy()


#Time to get out Gui
class FrameClass(wx.Frame): #Finally making the gui!
    def __init__(self,parent,title): #making init!
        app = wx.Frame
        app.__init__(self,parent,title=WxTitle,size = (340,280)) #set window size

        Menu = wx.Menu() #Lets make a menu!
        panel = wx.Panel(self) #set the panel var

        contact = Menu.Append(wx.ID_NONE,"&Contact Info") #add update thing
        self.Bind(wx.EVT_MENU,self.contact1,contact) #Add event for Update
        fwMsg = wx.StaticText(panel, label='Firmware', pos=(59,25))
        fwlist = wx.ComboBox(panel,pos=(118,22), choices=["6.60","6.60 Go/N1000","6.39","6.39 Go/N1000","6.35 Go/N1000","5.50","5.00","4.01"])
        self.Bind(wx.EVT_COMBOBOX, self.getsel, fwlist)
        downloadbutton = wx.Button(panel, label="Download FW", pos=(100,76))
        self.Bind(wx.EVT_BUTTON, self.DLB, downloadbutton)

        #now for the member!
        TopM = wx.MenuBar()
        TopM.Append(Menu, "Tool Opt")
        self.SetMenuBar(TopM)
        self.Show(True)

    def DLUpdate(self,e):
        #Check if DD equ true so we can post the MSG
        Download(Update_url)
        print "downloading"

        Done = wx.MessageDialog(self,"Download Done, download stored in \"DLBOXV$.zip\" file")
        Done.ShowModal()
        Done.Destroy()

    def contact1(self,e):
        con = wx.MessageDialog(self,Contact)
        con.ShowModal()
        con.Destroy()


    def getsel(self,e):
        i = e.GetString()
        if i == "6.60":
            e6602 = True
            print e6602,"660"
        else:
            e6602 = False
            print e6602,"660"

        if i == "6.60 Go/N1000":
            e6602g = True
            print e6602g,"660 go"
        else:
            e6602g = False
            print e6602g,"660 go"

        if i == "6.39":
            e6392 = True
            print e6392,"639"
        else:
            e6392 = False
            print e6392,"639"

        if i == "6.39 Go/N1000":
            e6392g = True
            print e6392g,"639 go"
        else:
            e6392g = False
            print e6392g,"639 go"

        if i == "6.35 Go/N1000":
            e6352g = False
            print e6352g,"635 go"
        else:
            e6352g = False
            print e6352g,"635 go"

        if i == "5.50":
            e5502 = True
            print e5502,"550"
        else:
            e5502 = False
            print e5502,"550"

        if i == "500":
            e5002 = True
            print e5002,"500"
        else:
            e5002 = False
            print e5002,"500"

        if i == "401":
            e4012 = True
            print e4012,"401"
        else:
            e4012 = False
            print e4012,"401"



    def DLB(self,e):
        if e6602 == True:
            Download(e660)
        elif e6602g == True:
            Download(e660g)
        elif e6392 == True:
            Download(e639)
        elif e639g == True:
            Download(e639g)
        elif e6352g == True:
            Download(e635g)
        elif e5502 == True:
            Download(e550)
        elif e5002 == True:
            Download(e500)
        elif e4012 == True:
            Download(e401)

G = wx.App(False)
Win = FrameClass(None,WxTitle)
G.MainLoop()

但在函数Download(url)将不起作用,它将不下载

def Download(url):
        response = urllib.urlopen(url).read()
        doned = wx.MessageDialog("Download Done")
        doned.ShowModal()
        doned.Destroy()

触发Download(url)的是一些if和elsif语句

def getsel(self,e):
            i = e.GetString()
            if i == "6.60":
                e6602 = True
                print e6602,"660"
            else:
                e6602 = False
                print e6602,"660"

            if i == "6.60 Go/N1000":
                e6602g = True
                print e6602g,"660 go"
            else:
                e6602g = False
                print e6602g,"660 go"

            if i == "6.39":
                e6392 = True
                print e6392,"639"
            else:
                e6392 = False
                print e6392,"639"

            if i == "6.39 Go/N1000":
                e6392g = True
                print e6392g,"639 go"
            else:
                e6392g = False
                print e6392g,"639 go"

            if i == "6.35 Go/N1000":
                e6352g = False
                print e6352g,"635 go"
            else:
                e6352g = False
                print e6352g,"635 go"

            if i == "5.50":
                e5502 = True
                print e5502,"550"
            else:
                e5502 = False
                print e5502,"550"

            if i == "500":
                e5002 = True
                print e5002,"500"
            else:
                e5002 = False
                print e5002,"500"

            if i == "401":
                e4012 = True
                print e4012,"401"
            else:
                e4012 = False
                print e4012,"401"



        def DLB(self,e):
            if e6602 == True:
                Download(e660)
            elif e6602g == True:
                Download(e660g)
            elif e6392 == True:
                Download(e639)
            elif e639g == True:
                Download(e639g)
            elif e6352g == True:
                Download(e635g)
            elif e5502 == True:
                Download(e550)
            elif e5002 == True:
                Download(e500)
            elif e4012 == True:
                Download(e401)

Tags: selffalsetruegogetifdownloadelse
1条回答
网友
1楼 · 发布于 2024-04-28 22:59:31

What did go wrong? I used urllib should I have used urllib2?

是的。只需在代码中添加2就可以解决问题,这一事实显然证明了这一点,但并不能解释太多。你知道吗

正如^{}的文档所说:

Deprecated since version 2.6: The urlopen() function has been removed in Python 3 in favor of urllib2.urlopen().

这并不意味着他们从2.6开始就停止修复urllib中的bug(直到2.7.9才有bug修复),但这确实意味着永远不会添加缺少的功能。其中包括一些类型的HTTP身份验证、重定向、带有非标准CAs的HTTPS、代理设置和代理身份验证。你知道吗

除了“下载没有发生”或者您试图下载的URL(您似乎传入了一个名为Update_box的变量,该变量没有分配到任何地方)或者您正在运行的安装程序之外,如果不知道任何问题,就不可能确切地知道这些问题中的哪一个(或任何其他问题)是关键。你知道吗

相关问题 更多 >