Skip to content

Instantly share code, notes, and snippets.

@glowinthedark
Created June 5, 2019 19:49
Show Gist options
  • Select an option

  • Save glowinthedark/7af3337f3b570a71589793a38d695fda to your computer and use it in GitHub Desktop.

Select an option

Save glowinthedark/7af3337f3b570a71589793a38d695fda to your computer and use it in GitHub Desktop.
Twisted SSL fileserver
#!/usr/bin/env python
# http://blog.vrplumber.com/index.php?/archives/1638-HowTo-Create-an-SSL-web-server-in-Twisted-Crude-approach,-but-it-works....html
# AND http://twistedmatrix.com/documents/current/web/howto/using-twistedweb.html#auto2
# Resource from: http://jcalderone.livejournal.com/54128.html
# ##### FOR HTTP YOU CAN USE INSTEAD THIS:
# ####### twistd -n web --path /
# GENERATE SSL CERTIFICATES:
# mkdir -p ~/.ssl && cd ~/.ssl
# openssl genrsa > privkey.pem
# openssl req -new -x509 -key privkey.pem -out cacert.pem -days 9999
# INSTALL openSSL with:
# pip install pyOpenSSL
# INSTALL service_identity
# ############## pip install service_identity
PORT = 443
import os
import sys
from os.path import expanduser
from twisted.internet import reactor, ssl
from twisted.python.log import startLogging
from twisted.web.server import Site
from twisted.web.static import File
startLogging(sys.stdout)
home_dir = expanduser("~")
sslContext = ssl.DefaultOpenSSLContextFactory(
os.path.join(home_dir, '.ssh/ssl/privkey.pem'),
os.path.join(home_dir, '.ssh/ssl/cacert.pem'),
)
# static http://twistedmatrix.com/documents/10.1.0/web/howto/web-in-60/static-content.html
if len(sys.argv) > 1:
root = sys.argv[1]
else:
root = '.'
resource = File(root)
site = Site(resource)
reactor.listenSSL(
PORT, # integer port
site, # our site object, see the web howto: http://twistedmatrix.com/documents/current/web/howto/web-overview.html
contextFactory=sslContext,
)
reactor.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment