2014-09-26 47 views
0

這裏是我的代碼,所有的URL都是Config Parser格式的文件。當按下按鈕時文件不會下載。出了什麼問題?我使用urllib應該使用urllib2嗎?一些功能可能在那裏,但沒有使用,只是忽略了。Python - 文件不會下載

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)是少數,如果和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) 
+2

將代碼修剪爲一個仍然存在問題的最小示例。 – Andy 2014-09-27 00:28:53

+0

是的,你應該使用'urllib2',因爲'urllib'已被棄用,而且你沒有使用任何不平凡的功能。然而,與您的問題有任何關係的可能性似乎非常小。 – abarnert 2014-09-27 00:32:15

+0

好的,謝謝你的建議和意見。 – MaoKnight 2014-09-27 04:41:48

回答

0

什麼出問題?我使用urllib應該使用urllib2嗎?

是。事實上,只要將2添加到代碼中就可以解決問題,這顯然證明了這一點,但這並不能說明問題。

隨着文檔的urllib.urlopen說:自從2.6版本

推薦使用:該urlopen()功能在Python 3贊成urllib2.urlopen()被刪除。

這並不意味着他們停止在固定的錯誤urllib爲2.6(有一個bug修復遲至2.7.9),但它確實意味着缺少的功能將永遠不會被添加。其中包括某些類型的HTTP身份驗證,重定向,帶有非標準CA的HTTPS,代理設置和代理身份驗證。

除了「下載沒有發生」或者您嘗試下載的URL(您似乎正在傳入名爲Update_box的變量並未分配到任何地方)之外,不知道任何有關此問題的任何信息,或者您正在運行的設置,不可能確切地知道這些問題中的哪一個(或任何其他問題)是關鍵。