diff options
author | Alex Gaynor <alex.gaynor@gmail.com> | 2019-02-10 17:23:19 -0500 |
---|---|---|
committer | Paul Kehrer <paul.l.kehrer@gmail.com> | 2019-02-10 17:23:19 -0500 |
commit | ca749b54ecca378e704ede349c9c9930732af877 (patch) | |
tree | fd16744a90033396b15bb313eaf82988c69c770a | |
parent | 01f90a1b010754c37a332b1b432bca73f7c529a2 (diff) | |
download | pyopenssl-git-ca749b54ecca378e704ede349c9c9930732af877.tar.gz |
Make all of the examples py3 syntax friendly (#816)
-rw-r--r-- | examples/proxy.py | 20 | ||||
-rw-r--r-- | examples/sni/client.py | 10 | ||||
-rw-r--r-- | examples/sni/server.py | 6 |
3 files changed, 21 insertions, 15 deletions
diff --git a/examples/proxy.py b/examples/proxy.py index 3be26f9..946942e 100644 --- a/examples/proxy.py +++ b/examples/proxy.py @@ -8,6 +8,8 @@ # # $Id: proxy.py,v 1.2 2004/07/22 12:01:25 martin Exp $ +from __future__ import print_function + import sys import socket import string @@ -16,9 +18,9 @@ from OpenSSL import SSL def usage(exit_code=0): - print "Usage: %s server[:port] proxy[:port]" % sys.argv[0] - print " Connects SSL to the specified server (port 443 by default)" - print " using the specified proxy (port 8080 by default)" + print("Usage: %s server[:port] proxy[:port]" % sys.argv[0]) + print(" Connects SSL to the specified server (port 443 by default)") + print(" using the specified proxy (port 8080 by default)") sys.exit(exit_code) @@ -44,13 +46,13 @@ def run(server, proxy): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s.connect(proxy) - except socket.error, e: - print "Unable to connect to %s:%s %s" % (proxy[0], proxy[1], str(e)) + except socket.error as e: + print("Unable to connect to %s:%s %s" % (proxy[0], proxy[1], str(e))) sys.exit(-1) # Use the CONNECT method to get a connection to the actual server s.send("CONNECT %s:%s HTTP/1.0\n\n" % (server[0], server[1])) - print "Proxy response: %s" % string.strip(s.recv(1024)) + print("Proxy response: %s" % string.strip(s.recv(1024))) ctx = SSL.Context(SSL.SSLv23_METHOD) conn = SSL.Connection(ctx, s) @@ -61,8 +63,8 @@ def run(server, proxy): # start using HTTP conn.send("HEAD / HTTP/1.0\n\n") - print "Sever response:" - print "-" * 40 + print("Sever response:") + print("-" * 40) while 1: try: buff = conn.recv(4096) @@ -70,7 +72,7 @@ def run(server, proxy): # we're done break - print buff, + print(buff, end="") if __name__ == '__main__': diff --git a/examples/sni/client.py b/examples/sni/client.py index 428525b..d4a751f 100644 --- a/examples/sni/client.py +++ b/examples/sni/client.py @@ -1,6 +1,8 @@ # Copyright (C) Jean-Paul Calderone # See LICENSE for details. +from __future__ import print_function + from sys import argv, stdout from socket import socket @@ -13,21 +15,21 @@ def main(): by argv[1], of it. """ if len(argv) < 2: - print 'Usage: %s <hostname>' % (argv[0],) + print('Usage: %s <hostname>' % (argv[0],)) return 1 client = socket() - print 'Connecting...', + print('Connecting...', end="") stdout.flush() client.connect(('127.0.0.1', 8443)) - print 'connected', client.getpeername() + print('connected', client.getpeername()) client_ssl = Connection(Context(TLSv1_METHOD), client) client_ssl.set_connect_state() client_ssl.set_tlsext_host_name(argv[1]) client_ssl.do_handshake() - print 'Server subject is', client_ssl.get_peer_certificate().get_subject() + print('Server subject is', client_ssl.get_peer_certificate().get_subject()) client_ssl.close() diff --git a/examples/sni/server.py b/examples/sni/server.py index e0c159a..891c6c5 100644 --- a/examples/sni/server.py +++ b/examples/sni/server.py @@ -1,6 +1,8 @@ # Copyright (C) Jean-Paul Calderone # See LICENSE for details. +from __future__ import print_function + from sys import stdout from socket import SOL_SOCKET, SO_REUSEADDR, socket @@ -29,10 +31,10 @@ def main(): port.bind(('', 8443)) port.listen(3) - print 'Accepting...', + print('Accepting...', end="") stdout.flush() server, addr = port.accept() - print 'accepted', addr + print('accepted', addr) server_context = Context(TLSv1_METHOD) server_context.set_tlsext_servername_callback(pick_certificate) |