-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·53 lines (42 loc) · 1.7 KB
/
server.py
File metadata and controls
executable file
·53 lines (42 loc) · 1.7 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
52
53
#!/usr/bin/python3
from http.server import BaseHTTPRequestHandler, HTTPServer
import os
import time
hostName = ""
hostPort = 80
class MyServer(BaseHTTPRequestHandler):
def do_HEAD(self):
self.send_response(200)
self.end_headers()
self.wfile.close()
def do_GET(self):
self.send_response(200)
if self.path.startswith('/mail/config-v1.1.xml'):
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'autoconfig.xml')
with open(path) as f:
template = f.read()
self.send_header('Content-type', 'text/xml')
self.end_headers()
context = {
'hostname': self.headers['Host'].replace('autoconfig.', ''),
'display_name': os.environ.get('DISPLAY_NAME', ''),
'display_short_name': os.environ.get('DISPLAY_SHORT_NAME', ''),
'mail_hostname': os.environ.get('MAIL_HOSTNAME', ''),
}
content = template.format(**context)
self.wfile.write(content.encode('utf-8'))
else:
self.send_header('Content-type', 'text/plain')
self.end_headers()
self.wfile.write('Autoconfig server (https://developer.mozilla.org/en-US/docs/Mozilla/Thunderbird/Autoconfiguration)\r\n'.encode('utf-8'))
def run():
myServer = HTTPServer((hostName, hostPort), MyServer)
print(time.asctime(), "Server started - %s:%s" % (hostName, hostPort))
try:
myServer.serve_forever()
except KeyboardInterrupt:
pass
myServer.server_close()
print(time.asctime(), "Server stopped - %s:%s" % (hostName, hostPort))
if __name__ == '__main__':
run()