/usr/share/doc/python-m2crypto/examples/medusa/https_server.py is in python-m2crypto 0.22.6~rc4-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | #!/usr/bin/env python
"""A https server built on Medusa's http_server.
Copyright (c) 1999-2003 Ng Pheng Siong. All rights reserved."""
import asynchat, asyncore, http_server, socket, sys
from M2Crypto import SSL
VERSION_STRING='0.09'
class https_channel(http_server.http_channel):
def __init__(self, server, conn, addr):
http_server.http_channel.__init__(self, server, conn, addr)
def send(self, data):
try:
result = self.socket._write_nbio(data)
if result <= 0:
return 0
else:
self.server.bytes_out.increment(result)
return result
except SSL.SSLError, why:
self.close()
self.log_info('send: closing channel %s %s' % (repr(self), why))
return 0
def recv(self, buffer_size):
try:
result = self.socket._read_nbio(buffer_size)
if result is None:
return ''
elif result == '':
self.close()
return ''
else:
self.server.bytes_in.increment(len(result))
return result
except SSL.SSLError, why:
self.close()
self.log_info('recv: closing channel %s %s' % (repr(self), why))
return ''
class https_server(http_server.http_server):
SERVER_IDENT='M2Crypto HTTPS Server (v%s)' % VERSION_STRING
channel_class=https_channel
def __init__(self, ip, port, ssl_ctx, resolver=None, logger_object=None):
http_server.http_server.__init__(self, ip, port, resolver, logger_object)
sys.stdout.write(self.SERVER_IDENT + '\n\n')
sys.stdout.flush()
self.ssl_ctx=ssl_ctx
def handle_accept(self):
# Cribbed from http_server.
self.total_clients.increment()
try:
conn, addr = self.accept()
except socket.error:
# linux: on rare occasions we get a bogus socket back from
# accept. socketmodule.c:makesockaddr complains that the
# address family is unknown. We don't want the whole server
# to shut down because of this.
sys.stderr.write ('warning: server accept() threw an exception\n')
return
# Turn the vanilla socket into an SSL connection.
try:
ssl_conn=SSL.Connection(self.ssl_ctx, conn)
ssl_conn._setup_ssl(addr)
ssl_conn.accept_ssl()
self.channel_class(self, ssl_conn, addr)
except SSL.SSLError:
pass
def writeable(self):
return 0
|