Skip to content

Instantly share code, notes, and snippets.

@DanielleSucher
Created April 13, 2012 20:15
Show Gist options
  • Save DanielleSucher/2379817 to your computer and use it in GitHub Desktop.
Save DanielleSucher/2379817 to your computer and use it in GitHub Desktop.
gevent (py3) test ssl server and client
# import gevent
# import gevent.socket as socket
# import gevent.ssl as ssl
# from gevent import spawn
import socket
import ssl
import pprint
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssl_sock = ssl.wrap_socket(sock,
ca_certs='keycert.pem', cert_reqs=ssl.CERT_REQUIRED)
ssl_sock.connect(('localhost', 9599))
ssl_sock.send(b"testing")
pprint.pprint(ssl_sock.getpeercert())
print (ssl_sock.recv(1024))
import gevent
import gevent.socket as socket
import gevent.ssl as ssl
from gevent import spawn
# import socket
# import ssl
server = socket.socket()
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(('', 9599))
server.listen(5)
ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
ctx.load_cert_chain(certfile='keycert.pem', keyfile='keycert.pem')
def handle_socket(sock):
while True:
# import pdb; pdb.set_trace()
d = sock.recv(1024)
print (d)
if not d:
break
sock.sendall(d)
while True:
ss = None
try:
sock, sockaddr = server.accept()
ss = ctx.wrap_socket(sock, server_side=True)
spawn(handle_socket, ss)
except Exception as e:
if not 'EOF' in str(e):
raise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment