sockets - Python program terminating unexpectedly -
i have program scrapes website , downloads files when finds it. runs fine @ other times flat out terminates operation of program before finishing searching sequence. i'm stumped. never quits while downloading while searching. i'm guessing socket error problem said above i'm stumped. i've put in httperror checking , nothing gets displayed http error code. i'm trying right insert socket error checking , gets crazier. prior adding socket error checking works fine. once add in socket error checking program won't run. brings idle display , shows cursor, idle ready , waiting next command. otherwise without socket error checking indicates program running until either tkinter window shuts down(err program terminates unexpectedly). if tkinter window shuts downs doesn't give error on idle.
what have find out why program terminating @ times , able trap out won't terminate , go , rerun same web address again. think have rerunning same web address taken care of don't have socket error handling correct, if it's socket error trouble. i'm stumped.
#!/usr/bin/python3.4 import urllib.request import os tkinter import * import time import urllib.error import errno root = tk() root.title("photodownloader") root.geometry("200x200") app = frame(root) app.grid() os.chdir('/home/someone/somewhere/') fileupdate = 10000 filecount = 19999 while fileupdate <= filecount: try: root.title(fileupdate) url = 'http://www.webpage.com/photos/'+str(fileupdate)+'.jpg' = urllib.request.urlopen(url) urllib.request.urlretrieve(url, str(fileupdate)+'.jpg') except urllib.error.httperror err: if err.code == 404: fileupdate = fileupdate + 1 root.update_idletasks() continue else: print(err.code) fileupdate = fileupdate + 1 root.update_idletasks() continue except socket.error, v: print(fileupdate, v[0]) continue fileupdate = fileupdate+1 root.update_idletasks()
i think problem caused tkinter not given chance start it's main event loop done when call root.mainloop()
, i'd recommend making code have in while
loop instead function periodically called root.after()
method. have included potential change test if fix issue.
note lines:
fileupdate = fileupdate + 1 root.update_idletasks() continue
in except branches redundant since happen if code kept going anyway, part of modifying code work in function rid of parts. here code i'd try running starting original while
statement:
#-while fileupdate <= filecount: def update_socket(): global fileupdate #allow global variable changed if fileupdate <= filecount: #/+ try: root.title(fileupdate) url = 'http://www.webpage.com/photos/'+str(fileupdate)+'.jpg' = urllib.request.urlopen(url) urllib.request.urlretrieve(url, str(fileupdate)+'.jpg') except urllib.error.httperror err: #<large redundant section removed> print("error code",err.code) except socket.error v: print("socket error",fileupdate, v[0]) #- continue root.after(500, update_socket) return #/+ fileupdate = fileupdate+1 #- root.update_idletasks() root.after(100, update_socket) #after 100 milliseconds call function again #change 100 smaller time call more frequently. root.after(0,update_socket) #when has chance, call update first time root.mainloop() #enter main event loop #/+
i indicate changed lines #-
followed chunk replaces ending #/+
Comments
Post a Comment