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:
parent
ade9d61c22
commit
3c1655322d
|
@ -63,9 +63,9 @@ if __name__ == '__main__':
|
||||||
(opts, args) = parser.parse_args()
|
(opts, args) = parser.parse_args()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if len(args) != 1: raise
|
if len(args) != 1: raise ValueError
|
||||||
opts.listen_port = int(args[0])
|
opts.listen_port = int(args[0])
|
||||||
except:
|
except ValueError:
|
||||||
parser.error("Invalid arguments")
|
parser.error("Invalid arguments")
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
|
@ -12,10 +12,9 @@ from websockify.websocket import WebSocket, \
|
||||||
parser = optparse.OptionParser(usage="%prog URL")
|
parser = optparse.OptionParser(usage="%prog URL")
|
||||||
(opts, args) = parser.parse_args()
|
(opts, args) = parser.parse_args()
|
||||||
|
|
||||||
try:
|
if len(args) == 1:
|
||||||
if len(args) != 1: raise
|
|
||||||
URL = args[0]
|
URL = args[0]
|
||||||
except:
|
else:
|
||||||
parser.error("Invalid arguments")
|
parser.error("Invalid arguments")
|
||||||
|
|
||||||
sock = WebSocket()
|
sock = WebSocket()
|
||||||
|
|
|
@ -105,7 +105,7 @@ class WebSocketLoad(WebSockifyRequestHandler):
|
||||||
cnt = int(cnt)
|
cnt = int(cnt)
|
||||||
length = int(length)
|
length = int(length)
|
||||||
chksum = int(chksum)
|
chksum = int(chksum)
|
||||||
except:
|
except ValueError:
|
||||||
print "\n<BOF>" + repr(data) + "<EOF>"
|
print "\n<BOF>" + repr(data) + "<EOF>"
|
||||||
err += "Invalid data format\n"
|
err += "Invalid data format\n"
|
||||||
continue
|
continue
|
||||||
|
@ -148,16 +148,16 @@ if __name__ == '__main__':
|
||||||
(opts, args) = parser.parse_args()
|
(opts, args) = parser.parse_args()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if len(args) != 1: raise
|
if len(args) != 1: raise ValueError
|
||||||
opts.listen_port = int(args[0])
|
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])
|
opts.listen_port = int(args[0])
|
||||||
if len(args) == 2:
|
if len(args) == 2:
|
||||||
opts.delay = int(args[1])
|
opts.delay = int(args[1])
|
||||||
else:
|
else:
|
||||||
opts.delay = 10
|
opts.delay = 10
|
||||||
except:
|
except ValueError:
|
||||||
parser.error("Invalid arguments")
|
parser.error("Invalid arguments")
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
|
@ -92,12 +92,5 @@ class ClientCertCNAuth(object):
|
||||||
self.source = src.split()
|
self.source = src.split()
|
||||||
|
|
||||||
def authenticate(self, headers, target_host, target_port):
|
def authenticate(self, headers, target_host, target_port):
|
||||||
try:
|
if headers.get('SSL_CLIENT_S_DN_CN', None) not in self.source:
|
||||||
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)
|
|
||||||
raise AuthenticationError(response_code=403)
|
raise AuthenticationError(response_code=403)
|
||||||
|
|
|
@ -32,7 +32,7 @@ class ReadOnlyTokenFile(BasePlugin):
|
||||||
try:
|
try:
|
||||||
tok, target = line.split(': ')
|
tok, target = line.split(': ')
|
||||||
self._targets[tok] = target.strip().rsplit(':', 1)
|
self._targets[tok] = target.strip().rsplit(':', 1)
|
||||||
except:
|
except ValueError:
|
||||||
print >>sys.stderr, "Syntax error in %s on line %d" % (self.source, index)
|
print >>sys.stderr, "Syntax error in %s on line %d" % (self.source, index)
|
||||||
index += 1
|
index += 1
|
||||||
|
|
||||||
|
|
|
@ -31,8 +31,10 @@ except ImportError:
|
||||||
numpy = None
|
numpy = None
|
||||||
|
|
||||||
# python 3.0 differences
|
# python 3.0 differences
|
||||||
try: from urllib.parse import urlparse
|
try:
|
||||||
except: from urlparse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
except ImportError:
|
||||||
|
from urlparse import urlparse
|
||||||
|
|
||||||
# SSLWant*Error is 2.7.9+
|
# SSLWant*Error is 2.7.9+
|
||||||
try:
|
try:
|
||||||
|
@ -40,7 +42,7 @@ try:
|
||||||
pass
|
pass
|
||||||
class WebSocketWantWriteError(ssl.SSLWantWriteError):
|
class WebSocketWantWriteError(ssl.SSLWantWriteError):
|
||||||
pass
|
pass
|
||||||
except:
|
except AttributeError:
|
||||||
class WebSocketWantReadError(OSError):
|
class WebSocketWantReadError(OSError):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
OSError.__init__(self, errno.EWOULDBLOCK)
|
OSError.__init__(self, errno.EWOULDBLOCK)
|
||||||
|
|
|
@ -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
|
import signal, socket, optparse, time, os, sys, subprocess, logging, errno
|
||||||
try: from socketserver import ForkingMixIn
|
try:
|
||||||
except: from SocketServer import ForkingMixIn
|
from socketserver import ForkingMixIn
|
||||||
try: from http.server import HTTPServer
|
except ImportError:
|
||||||
except: from BaseHTTPServer import HTTPServer
|
from SocketServer import ForkingMixIn
|
||||||
|
|
||||||
|
try:
|
||||||
|
from http.server import HTTPServer
|
||||||
|
except ImportError:
|
||||||
|
from BaseHTTPServer import HTTPServer
|
||||||
|
|
||||||
import select
|
import select
|
||||||
from websockify import websockifyserver
|
from websockify import websockifyserver
|
||||||
from websockify import auth_plugins as auth
|
from websockify import auth_plugins as auth
|
||||||
try:
|
try:
|
||||||
from urllib.parse import parse_qs, urlparse
|
from urllib.parse import parse_qs, urlparse
|
||||||
except:
|
except ImportError:
|
||||||
from cgi import parse_qs
|
from cgi import parse_qs
|
||||||
from urlparse import urlparse
|
from urlparse import urlparse
|
||||||
|
|
||||||
|
@ -70,7 +76,7 @@ Traffic Legend:
|
||||||
client_cert_subject = dict([x[0] for x in client_cert_subject])
|
client_cert_subject = dict([x[0] for x in client_cert_subject])
|
||||||
# add common name to headers (apache +StdEnvVars style)
|
# add common name to headers (apache +StdEnvVars style)
|
||||||
self.headers['SSL_CLIENT_S_DN_CN'] = client_cert_subject['commonName']
|
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
|
# not a SSL connection or client presented no certificate with valid data
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -117,14 +123,13 @@ Traffic Legend:
|
||||||
# Start proxying
|
# Start proxying
|
||||||
try:
|
try:
|
||||||
self.do_proxy(tsock)
|
self.do_proxy(tsock)
|
||||||
except:
|
finally:
|
||||||
if tsock:
|
if tsock:
|
||||||
tsock.shutdown(socket.SHUT_RDWR)
|
tsock.shutdown(socket.SHUT_RDWR)
|
||||||
tsock.close()
|
tsock.close()
|
||||||
if self.verbose:
|
if self.verbose:
|
||||||
self.log_message("%s:%s: Closed target",
|
self.log_message("%s:%s: Closed target",
|
||||||
self.server.target_host, self.server.target_port)
|
self.server.target_host, self.server.target_port)
|
||||||
raise
|
|
||||||
|
|
||||||
def get_target(self, target_plugin, path):
|
def get_target(self, target_plugin, path):
|
||||||
"""
|
"""
|
||||||
|
@ -509,8 +514,10 @@ def websockify_init():
|
||||||
else:
|
else:
|
||||||
opts.listen_host, opts.listen_port = '', arg
|
opts.listen_host, opts.listen_port = '', arg
|
||||||
|
|
||||||
try: opts.listen_port = int(opts.listen_port)
|
try:
|
||||||
except: parser.error("Error parsing listen port")
|
opts.listen_port = int(opts.listen_port)
|
||||||
|
except ValueError:
|
||||||
|
parser.error("Error parsing listen port")
|
||||||
|
|
||||||
del opts.inetd
|
del opts.inetd
|
||||||
|
|
||||||
|
@ -526,8 +533,11 @@ def websockify_init():
|
||||||
opts.target_host = opts.target_host.strip('[]')
|
opts.target_host = opts.target_host.strip('[]')
|
||||||
else:
|
else:
|
||||||
parser.error("Error parsing target")
|
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:
|
if len(args) > 0 and opts.wrap_cmd == None:
|
||||||
parser.error("Too many arguments")
|
parser.error("Too many arguments")
|
||||||
|
|
|
@ -10,8 +10,10 @@ Licensed under LGPL version 3 (see docs/LICENSE.LGPL-3)
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
# python 3.0 differences
|
# python 3.0 differences
|
||||||
try: from http.server import BaseHTTPRequestHandler, HTTPServer
|
try:
|
||||||
except: from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
|
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||||
|
except ImportError:
|
||||||
|
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
|
||||||
|
|
||||||
from websockify.websocket import WebSocket, WebSocketWantReadError, WebSocketWantWriteError
|
from websockify.websocket import WebSocket, WebSocketWantReadError, WebSocketWantWriteError
|
||||||
|
|
||||||
|
|
|
@ -22,8 +22,11 @@ if sys.hexversion > 0x3000000:
|
||||||
s2b = lambda s: s.encode('latin_1')
|
s2b = lambda s: s.encode('latin_1')
|
||||||
else:
|
else:
|
||||||
s2b = lambda s: s # No-op
|
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
|
# Degraded functionality if these imports are missing
|
||||||
for mod, msg in [('ssl', 'TLS/SSL/wss is disabled'),
|
for mod, msg in [('ssl', 'TLS/SSL/wss is disabled'),
|
||||||
|
|
Loading…
Reference in New Issue