Ich bin ein bisschen ein Python-Neuling und dies ist mein erster Beitrag zu stackoverflow also bitte bitte mit mir. :) Vor dem Posten habe ich google und stackoverflow gesucht, aber kann anscheinend nichts finden, das meinem Problem ähnlich ist.Python 3.5.2: socket.timeout Ausnahme verursacht typeserror
Ich habe ein Skript, das eine Website abfragt und den Inhalt abruft. Es funktioniert stundenlang, aber wenn es ein Socket-Timeout auftritt, löst das Skript einen Typfehler aus, obwohl ich eine Ausnahme dafür habe.
Ich bin sicher, ich vermisse etwas Offensichtliches, kann aber nicht meinen Finger darauf legen.
Code:
timingout = 10
def get_url(url):
try:
sock = urllib.request.urlopen(url, timeout=timingout)
orig_html = sock.read()
html = orig_html.decode("utf-8", errors="ignore").encode('cp1252', errors='ignore')
sock.close()
except KeyboardInterrupt:
# Kill program if Control-C is pressed
sys.exit(0)
except urllib.error.URLError as e:
print("***Error= Page ", e.reason)
return
except timingout:
print("socket timed out - URL: %s", url)
else:
# See if site is Down or errors eg: 404
if html == None:
print ("page contains no content!?!")
return ''
# See if site is complaining
elif html == site_overload:
if _verbose:
print('ERROR: Too many requests - SLEEPING 600 secs')
time.sleep(600)
return ''
# If not, we are good
elif html:
return html
Fehler:
return self._sock.recv_into(b)
**socket.timeout: timed out**
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test.py", line 201, in <module>
main()
File "test.py", line 140, in main
http_text = get_text(site_id)
File "test.py", line 110, in get_text
return get_url(url)
File "test.py", line 59, in get_url
except timingout:
**TypeError: catching classes that do not inherit from BaseException is not allowed**
Vielen Dank im Voraus für alle Anregungen & Hilfe!
Danke für die schnelle Antwort. ich sehe, was ich jetzt getan habe. Ich werde auch versuchen, Ihrem Rat zu folgen, die Versuchserklärungen aufzubrechen. Ich bin sehr neu in diesem so wirklich schätzen die Führung :) – GpT