Python - 文件不会下载

时间:2014-09-26 23:29:18

标签: python wxpython urllib

这是我的代码,所有网址都是Config Parser格式文件。按下按钮时,文件将无法下载。出了什么问题?我用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)

1 个答案:

答案 0 :(得分:0)

  

出了什么问题?我用urllib应该使用urllib2吗?

是。只需在您的代码中添加2即可修复此问题,这一事实显然证明了这一点,但它并没有解释清楚。

正如urllib.urlopen的文档所说:

  

从版本2.6开始不推荐使用:在Python 3中删除了urlopen()函数,转而使用urllib2.urlopen()

这并不意味着他们在2.6版本的urllib中停止修复错误(有一个错误修复,直到2.7.9),但这确实意味着永远不会添加缺少的功能。其中包括一些HTTP身份验证,重定向,使用非标准CA的HTTPS,代理设置和代理身份验证。

除了&#34之外什么都不知道问题;下载没有发生",或者您尝试下载的URL(您似乎传递了一个名为{的变量{1}}没有在任何地方分配,或者您正在运行的设置,无法确切地知道这些问题中的哪一个(或任何其他问题)是关键。