Skip to content

Commit 1cf8744

Browse files
committed
<Add> : File Server code With OpenSSL
1 parent 741e441 commit 1cf8744

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

fileServer.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import http.server, ssl, os
2+
3+
# absolute path of pyServer.py
4+
thisScriptPath=os.path.dirname(os.path.abspath(__file__))+'/'
5+
6+
# generated seld signed certificate usin openssl command
7+
def generate_selfsigned_cert():
8+
try:
9+
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"'
10+
os.system(OpenSslCommand)
11+
print('<<<<Certificate Generated>>>>>>')
12+
except:
13+
print('error while generating certificate')
14+
15+
# starts server on provided host and port
16+
def startServer(host,port):
17+
server_address = (host, port)
18+
httpd = http.server.HTTPServer(server_address, http.server.SimpleHTTPRequestHandler)
19+
httpd.socket = ssl.wrap_socket(httpd.socket,
20+
server_side=True,
21+
certfile = thisScriptPath+"cert.pem",
22+
keyfile = thisScriptPath+"key.pem",
23+
ssl_version=ssl.PROTOCOL_TLSv1
24+
)
25+
print("File Server started at https://" + server_address[0]+":"+str(server_address[1]))
26+
httpd.serve_forever()
27+
28+
def main():
29+
try:
30+
generate_selfsigned_cert()
31+
# you can change the host and port
32+
startServer('localhost',8000)
33+
except KeyboardInterrupt:
34+
print("\nFile Server Stopped!")
35+
36+
# entry point of script
37+
main()
38+
39+
'''
40+
Command reference for signed certificate generation:
41+
1) openssl req -newkey rsa:4096 \
42+
-x509 \
43+
-sha256 \
44+
-days 3650 \
45+
-nodes \
46+
-out cert.pem \
47+
-keyout key.pem \
48+
-subj "/C=IN/ST=Maharashtra/L=Satara/O=Wannabees/OU=KahiHiHa Department/CN=www.iamselfdepartment.com"
49+
50+
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"
51+
'''

0 commit comments

Comments
 (0)