Skip to content

Commit ca749b5

Browse files
alexreaperhulk
authored andcommitted
Make all of the examples py3 syntax friendly (#816)
1 parent 01f90a1 commit ca749b5

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

examples/proxy.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#
99
# $Id: proxy.py,v 1.2 2004/07/22 12:01:25 martin Exp $
1010

11+
from __future__ import print_function
12+
1113
import sys
1214
import socket
1315
import string
@@ -16,9 +18,9 @@
1618

1719

1820
def usage(exit_code=0):
19-
print "Usage: %s server[:port] proxy[:port]" % sys.argv[0]
20-
print " Connects SSL to the specified server (port 443 by default)"
21-
print " using the specified proxy (port 8080 by default)"
21+
print("Usage: %s server[:port] proxy[:port]" % sys.argv[0])
22+
print(" Connects SSL to the specified server (port 443 by default)")
23+
print(" using the specified proxy (port 8080 by default)")
2224
sys.exit(exit_code)
2325

2426

@@ -44,13 +46,13 @@ def run(server, proxy):
4446
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
4547
try:
4648
s.connect(proxy)
47-
except socket.error, e:
48-
print "Unable to connect to %s:%s %s" % (proxy[0], proxy[1], str(e))
49+
except socket.error as e:
50+
print("Unable to connect to %s:%s %s" % (proxy[0], proxy[1], str(e)))
4951
sys.exit(-1)
5052

5153
# Use the CONNECT method to get a connection to the actual server
5254
s.send("CONNECT %s:%s HTTP/1.0\n\n" % (server[0], server[1]))
53-
print "Proxy response: %s" % string.strip(s.recv(1024))
55+
print("Proxy response: %s" % string.strip(s.recv(1024)))
5456

5557
ctx = SSL.Context(SSL.SSLv23_METHOD)
5658
conn = SSL.Connection(ctx, s)
@@ -61,16 +63,16 @@ def run(server, proxy):
6163
# start using HTTP
6264

6365
conn.send("HEAD / HTTP/1.0\n\n")
64-
print "Sever response:"
65-
print "-" * 40
66+
print("Sever response:")
67+
print("-" * 40)
6668
while 1:
6769
try:
6870
buff = conn.recv(4096)
6971
except SSL.ZeroReturnError:
7072
# we're done
7173
break
7274

73-
print buff,
75+
print(buff, end="")
7476

7577

7678
if __name__ == '__main__':

examples/sni/client.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Copyright (C) Jean-Paul Calderone
22
# See LICENSE for details.
33

4+
from __future__ import print_function
5+
46
from sys import argv, stdout
57
from socket import socket
68

@@ -13,21 +15,21 @@ def main():
1315
by argv[1], of it.
1416
"""
1517
if len(argv) < 2:
16-
print 'Usage: %s <hostname>' % (argv[0],)
18+
print('Usage: %s <hostname>' % (argv[0],))
1719
return 1
1820

1921
client = socket()
2022

21-
print 'Connecting...',
23+
print('Connecting...', end="")
2224
stdout.flush()
2325
client.connect(('127.0.0.1', 8443))
24-
print 'connected', client.getpeername()
26+
print('connected', client.getpeername())
2527

2628
client_ssl = Connection(Context(TLSv1_METHOD), client)
2729
client_ssl.set_connect_state()
2830
client_ssl.set_tlsext_host_name(argv[1])
2931
client_ssl.do_handshake()
30-
print 'Server subject is', client_ssl.get_peer_certificate().get_subject()
32+
print('Server subject is', client_ssl.get_peer_certificate().get_subject())
3133
client_ssl.close()
3234

3335

examples/sni/server.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Copyright (C) Jean-Paul Calderone
22
# See LICENSE for details.
33

4+
from __future__ import print_function
5+
46
from sys import stdout
57
from socket import SOL_SOCKET, SO_REUSEADDR, socket
68

@@ -29,10 +31,10 @@ def main():
2931
port.bind(('', 8443))
3032
port.listen(3)
3133

32-
print 'Accepting...',
34+
print('Accepting...', end="")
3335
stdout.flush()
3436
server, addr = port.accept()
35-
print 'accepted', addr
37+
print('accepted', addr)
3638

3739
server_context = Context(TLSv1_METHOD)
3840
server_context.set_tlsext_servername_callback(pick_certificate)

0 commit comments

Comments
 (0)