forked from VaradLanke/https-file-server-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileServer.py
More file actions
51 lines (44 loc) · 2.04 KB
/
Copy pathfileServer.py
File metadata and controls
51 lines (44 loc) · 2.04 KB
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
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"
'''