Polish up examples (somewhat)
- Mention them in the docs (arguably a bit hamfistedly).
- Make the README an RST.
- Make them pass flake8 and add flake8 to tox.ini
They should all be rewritten and made Python 3-friendly but that's out
of scope here.
diff --git a/examples/simple/client.py b/examples/simple/client.py
index dff2d03..5662122 100644
--- a/examples/simple/client.py
+++ b/examples/simple/client.py
@@ -8,8 +8,12 @@
Simple SSL client, using blocking I/O
"""
+import os
+import socket
+import sys
+
from OpenSSL import SSL, crypto
-import sys, os, select, socket
+
def verify_cb(conn, cert, errnum, depth, ok):
certsubject = crypto.X509Name(cert.get_subject())
@@ -17,20 +21,23 @@
print('Got certificate: ' + commonname)
return ok
+
if len(sys.argv) < 3:
print('Usage: python client.py HOST PORT')
sys.exit(1)
+
dir = os.path.dirname(sys.argv[0])
if dir == '':
dir = os.curdir
+
# Initialize context
ctx = SSL.Context(SSL.SSLv23_METHOD)
ctx.set_options(SSL.OP_NO_SSLv2)
ctx.set_options(SSL.OP_NO_SSLv3)
-ctx.set_verify(SSL.VERIFY_PEER, verify_cb) # Demand a certificate
-ctx.use_privatekey_file (os.path.join(dir, 'client.pkey'))
+ctx.set_verify(SSL.VERIFY_PEER, verify_cb) # Demand a certificate
+ctx.use_privatekey_file(os.path.join(dir, 'client.pkey'))
ctx.use_certificate_file(os.path.join(dir, 'client.cert'))
ctx.load_verify_locations(os.path.join(dir, 'CA.cert'))
diff --git a/examples/simple/server.py b/examples/simple/server.py
index bc001ed..19f6d21 100644
--- a/examples/simple/server.py
+++ b/examples/simple/server.py
@@ -8,16 +8,23 @@
Simple echo server, using nonblocking I/O
"""
+from __future__ import print_function
+
+import os
+import select
+import socket
+import sys
+
from OpenSSL import SSL, crypto
-import sys, os, select, socket
def verify_cb(conn, cert, errnum, depth, ok):
certsubject = crypto.X509Name(cert.get_subject())
commonname = certsubject.commonName
- print(('Got certificate: ' + commonname))
+ print('Got certificate: ' + commonname)
return ok
+
if len(sys.argv) < 2:
print('Usage: python server.py PORT')
sys.exit(1)
@@ -30,20 +37,23 @@
ctx = SSL.Context(SSL.SSLv23_METHOD)
ctx.set_options(SSL.OP_NO_SSLv2)
ctx.set_options(SSL.OP_NO_SSLv3)
-ctx.set_verify(SSL.VERIFY_PEER|SSL.VERIFY_FAIL_IF_NO_PEER_CERT, verify_cb) # Demand a certificate
-ctx.use_privatekey_file (os.path.join(dir, 'server.pkey'))
+ctx.set_verify(
+ SSL.VERIFY_PEER | SSL.VERIFY_FAIL_IF_NO_PEER_CERT, verify_cb
+) # Demand a certificate
+ctx.use_privatekey_file(os.path.join(dir, 'server.pkey'))
ctx.use_certificate_file(os.path.join(dir, 'server.cert'))
ctx.load_verify_locations(os.path.join(dir, 'CA.cert'))
# Set up server
server = SSL.Connection(ctx, socket.socket(socket.AF_INET, socket.SOCK_STREAM))
server.bind(('', int(sys.argv[1])))
-server.listen(3)
+server.listen(3)
server.setblocking(0)
clients = {}
writers = {}
+
def dropClient(cli, errors=None):
if errors:
print('Client %s left unexpectedly:' % (clients[cli],))
@@ -59,20 +69,24 @@
while 1:
try:
- r, w, _ = select.select([server] + list(clients.keys()), list(writers.keys()), [])
+ r, w, _ = select.select(
+ [server] + list(clients.keys()), list(writers.keys()), []
+ )
except:
break
for cli in r:
if cli == server:
- cli,addr = server.accept()
+ cli, addr = server.accept()
print('Connection from %s' % (addr,))
clients[cli] = addr
else:
try:
ret = cli.recv(1024).decode('utf-8')
- except (SSL.WantReadError, SSL.WantWriteError, SSL.WantX509LookupError):
+ except (SSL.WantReadError,
+ SSL.WantWriteError,
+ SSL.WantX509LookupError):
pass
except SSL.ZeroReturnError:
dropClient(cli)
@@ -86,7 +100,9 @@
for cli in w:
try:
ret = cli.send(writers[cli])
- except (SSL.WantReadError, SSL.WantWriteError, SSL.WantX509LookupError):
+ except (SSL.WantReadError,
+ SSL.WantWriteError,
+ SSL.WantX509LookupError):
pass
except SSL.ZeroReturnError:
dropClient(cli)