Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions fileServer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import http.server, ssl, os

# absolute path of pyServer.py
thisScriptPath=os.path.dirname(os.path.abspath(__file__))+'/'

# generated seld signed certificate usin openssl command
def generate_selfsigned_cert():
try:
OpenSslCommand = 'openssl req -newkey rsa:4096 -x509 -sha256 -days 3650 -nodes -out '+thisScriptPath+'cert.pem -keyout '+thisScriptPath+'key.pem -subj "/C=IN/ST=Maharashtra/L=Satara/O=Wannabees/OU=KahiHiHa Department/CN=www.iamselfdepartment.com"'
os.system(OpenSslCommand)
print('<<<<Certificate Generated>>>>>>')
except:
print('error while generating certificate')

# starts server on provided host and port
def startServer(host,port):
server_address = (host, port)
httpd = http.server.HTTPServer(server_address, http.server.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket(httpd.socket,
server_side=True,
certfile = thisScriptPath+"cert.pem",
keyfile = thisScriptPath+"key.pem",
ssl_version=ssl.PROTOCOL_TLSv1
)
print("File Server started at https://" + server_address[0]+":"+str(server_address[1]))
httpd.serve_forever()

def main():
try:
generate_selfsigned_cert()
# you can change the host and port
startServer('localhost',8000)
except KeyboardInterrupt:
print("\nFile Server Stopped!")

# entry point of script
main()

'''
Command reference for signed certificate generation:
1) openssl req -newkey rsa:4096 \
-x509 \
-sha256 \
-days 3650 \
-nodes \
-out cert.pem \
-keyout key.pem \
-subj "/C=IN/ST=Maharashtra/L=Satara/O=Wannabees/OU=KahiHiHa Department/CN=www.iamselfdepartment.com"

2) openssl req -newkey rsa:4096 -x509 -sha256 -days 3650 -nodes -out cert.pem -keyout key.pem -subj "/C=IN/ST=Maharashtra/L=Satara/O=Wannabees/OU=KahiHiHa Department/CN=www.iamselfdepartment.com"
'''