Do not use base except: clauses

https://docs.python.org/2/howto/doanddont.html#except

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
This commit is contained in:
Anders Kaseorg 2017-11-10 05:18:32 -05:00
parent ade9d61c22
commit 3c1655322d
9 changed files with 46 additions and 37 deletions

View File

@ -63,9 +63,9 @@ if __name__ == '__main__':
(opts, args) = parser.parse_args()
try:
if len(args) != 1: raise
if len(args) != 1: raise ValueError
opts.listen_port = int(args[0])
except:
except ValueError:
parser.error("Invalid arguments")
logging.basicConfig(level=logging.INFO)

View File

@ -12,10 +12,9 @@ from websockify.websocket import WebSocket, \
parser = optparse.OptionParser(usage="%prog URL")
(opts, args) = parser.parse_args()
try:
if len(args) != 1: raise
if len(args) == 1:
URL = args[0]
except:
else:
parser.error("Invalid arguments")
sock = WebSocket()

View File

@ -105,7 +105,7 @@ class WebSocketLoad(WebSockifyRequestHandler):
cnt = int(cnt)
length = int(length)
chksum = int(chksum)
except:
except ValueError:
print "\n<BOF>" + repr(data) + "<EOF>"
err += "Invalid data format\n"
continue
@ -148,16 +148,16 @@ if __name__ == '__main__':
(opts, args) = parser.parse_args()
try:
if len(args) != 1: raise
if len(args) != 1: raise ValueError
opts.listen_port = int(args[0])
if len(args) not in [1,2]: raise
if len(args) not in [1,2]: raise ValueError
opts.listen_port = int(args[0])
if len(args) == 2:
opts.delay = int(args[1])
else:
opts.delay = 10
except:
except ValueError:
parser.error("Invalid arguments")
logging.basicConfig(level=logging.INFO)

View File

@ -92,12 +92,5 @@ class ClientCertCNAuth(object):
self.source = src.split()
def authenticate(self, headers, target_host, target_port):
try:
if (headers.get('SSL_CLIENT_S_DN_CN') not in self.source):
raise AuthenticationError(response_code=403)
except AuthenticationError:
# re-raise AuthenticationError (raised by common name not in configured source list)
raise
except:
# deny access in case any error occurs (i.e. no data provided)
if headers.get('SSL_CLIENT_S_DN_CN', None) not in self.source:
raise AuthenticationError(response_code=403)

View File

@ -32,7 +32,7 @@ class ReadOnlyTokenFile(BasePlugin):
try:
tok, target = line.split(': ')
self._targets[tok] = target.strip().rsplit(':', 1)
except:
except ValueError:
print >>sys.stderr, "Syntax error in %s on line %d" % (self.source, index)
index += 1

View File

@ -31,8 +31,10 @@ except ImportError:
numpy = None
# python 3.0 differences
try: from urllib.parse import urlparse
except: from urlparse import urlparse
try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse
# SSLWant*Error is 2.7.9+
try:
@ -40,7 +42,7 @@ try:
pass
class WebSocketWantWriteError(ssl.SSLWantWriteError):
pass
except:
except AttributeError:
class WebSocketWantReadError(OSError):
def __init__(self):
OSError.__init__(self, errno.EWOULDBLOCK)

View File

@ -12,16 +12,22 @@ as taken from http://docs.python.org/dev/library/ssl.html#certificates
'''
import signal, socket, optparse, time, os, sys, subprocess, logging, errno
try: from socketserver import ForkingMixIn
except: from SocketServer import ForkingMixIn
try: from http.server import HTTPServer
except: from BaseHTTPServer import HTTPServer
try:
from socketserver import ForkingMixIn
except ImportError:
from SocketServer import ForkingMixIn
try:
from http.server import HTTPServer
except ImportError:
from BaseHTTPServer import HTTPServer
import select
from websockify import websockifyserver
from websockify import auth_plugins as auth
try:
from urllib.parse import parse_qs, urlparse
except:
except ImportError:
from cgi import parse_qs
from urlparse import urlparse
@ -70,7 +76,7 @@ Traffic Legend:
client_cert_subject = dict([x[0] for x in client_cert_subject])
# add common name to headers (apache +StdEnvVars style)
self.headers['SSL_CLIENT_S_DN_CN'] = client_cert_subject['commonName']
except:
except (TypeError, AttributeError, KeyError):
# not a SSL connection or client presented no certificate with valid data
pass
@ -117,14 +123,13 @@ Traffic Legend:
# Start proxying
try:
self.do_proxy(tsock)
except:
finally:
if tsock:
tsock.shutdown(socket.SHUT_RDWR)
tsock.close()
if self.verbose:
self.log_message("%s:%s: Closed target",
self.server.target_host, self.server.target_port)
raise
def get_target(self, target_plugin, path):
"""
@ -509,8 +514,10 @@ def websockify_init():
else:
opts.listen_host, opts.listen_port = '', arg
try: opts.listen_port = int(opts.listen_port)
except: parser.error("Error parsing listen port")
try:
opts.listen_port = int(opts.listen_port)
except ValueError:
parser.error("Error parsing listen port")
del opts.inetd
@ -526,8 +533,11 @@ def websockify_init():
opts.target_host = opts.target_host.strip('[]')
else:
parser.error("Error parsing target")
try: opts.target_port = int(opts.target_port)
except: parser.error("Error parsing target port")
try:
opts.target_port = int(opts.target_port)
except ValueError:
parser.error("Error parsing target port")
if len(args) > 0 and opts.wrap_cmd == None:
parser.error("Too many arguments")

View File

@ -10,8 +10,10 @@ Licensed under LGPL version 3 (see docs/LICENSE.LGPL-3)
import sys
# python 3.0 differences
try: from http.server import BaseHTTPRequestHandler, HTTPServer
except: from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
try:
from http.server import BaseHTTPRequestHandler, HTTPServer
except ImportError:
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from websockify.websocket import WebSocket, WebSocketWantReadError, WebSocketWantWriteError

View File

@ -22,8 +22,11 @@ if sys.hexversion > 0x3000000:
s2b = lambda s: s.encode('latin_1')
else:
s2b = lambda s: s # No-op
try: from http.server import SimpleHTTPRequestHandler
except: from SimpleHTTPServer import SimpleHTTPRequestHandler
try:
from http.server import SimpleHTTPRequestHandler
except ImportError:
from SimpleHTTPServer import SimpleHTTPRequestHandler
# Degraded functionality if these imports are missing
for mod, msg in [('ssl', 'TLS/SSL/wss is disabled'),