Giter VIP home page Giter VIP logo

sslserver's Introduction

sslserver

sslserver adds SSL support for classes in socketserver

Installation

pip install git+https://github.com/Y4hL/sslserver

sslserver Objects

sslserver.TCPServer

sslserver.TCPServer attempts to be a dropin replacement for the socketserver.TCPServer class. The only change is a added parameter context.

class TCPServer(server_address, RequestHandlerClass, bind_and_activate=True, context=None)

Passing context a valid SSLContext object will enable SSL on the server.
If context is not passed, a warning will be logged, but the server will run without SSL.

sslserver.ThreadingTCPServer

sslservers.ThreadingTCPServer is a copy of socketserver.ThreadingTCPServer but inherited from sslservers.TCPServer.

sslserver.ForkingTCPServer

sslserver.ForkingTCPServer is a copy of socketserver.ForkingTCPServer but inherited from sslservers.TCPServer.

sslserver.ThreadPoolMixIn

ThreadPoolMixIn uses concurrent.futures.ThreadPoolExecutor to process requests.
While socketserver.ThreadingMixIn creates a thread for each connection, ThreadPoolMixIn reuses a set amount of threads.
As with socketserver.ThreadingMixIn, ThreadPoolMixIn can be used with both socketserver.TCPServer and sslserver.TCPServer

Examples

TCPServer example from the python documentation using sslserver

import ssl
import sslserver
import socketserver

class MyTCPHandler(socketserver.BaseRequestHandler):
    """
    The request handler class for our server.

    It is instantiated once per connection to the server, and must
    override the handle() method to implement communication to the
    client.
    """

    def handle(self):
        # self.request is the TCP socket connected to the client
        self.data = self.request.recv(1024).strip()
        print(f"{self.client_address[0]} wrote: {self.data}")
        # just send back the same data, but upper-cased
        self.request.sendall(self.data.upper())

if __name__ == "__main__":
    address = ("localhost", 9999)

    # Create default SSLContext
    context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
    context.load_cert_chain("server.cert", "server.key")

    # Create the server, binding to localhost on port 9999
    with sslserver.TCPServer(address, MyTCPHandler, context=context) as server:
        # Activate the server; this will keep running until you
        # interrupt the program with Ctrl-C
        server.serve_forever()

ThreadPoolMixIn example

import sslserver
import socketserver

# Use sslserver.ThreadPoolMixIn to create new server class
# socketserver.TCPServer can be changed to sslserver.TCPServer for ssl support
class ThreadPoolTCPServer(sslserver.ThreadPoolMixIn, sslserver.TCPServer):
    """ socketserver.TCPServer using a ThreadPool """

class MyTCPHandler(socketserver.BaseRequestHandler):
    """ Handler class for incoming connections """

    def handle(self):
        self.data = self.request.recv(1024)
        print(f"{self.client_address[0]} wrote: {self.data}")
        # Send data back in uppercase
        self.request.sendall(self.data.upper())

if __name__ == "__main__":
    address = ("localhost", 9999)

    with ThreadPoolTCPServer(address, MyHandler) as server:
        server.serve_forever()

ThreadPoolMixIn with custom amount of threads

TheadPoolMixIn by default lets concurrent.futures.ThreadPoolExecutor choose the amount of threads it uses.
However if one wants to overwrite this value, it can be done before calling serve_forever()

from concurrent.futures import ThreadPoolExecutor

if __name__ == "__main__":
    address = ("localhost", 9999)

    thread_count = 32

    with ThreadPoolTCPServer(address, MyHandler) as server:
        # Overwrite executor
        server.executor = ThreadPoolExecutor(max_workers=thread_count)
        server.serve_forever()

sslserver's People

Contributors

y4hl avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.