File: http-server.py

package info (click to toggle)
casync 2%2B20180321-2.1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,876 kB
  • sloc: ansic: 30,815; sh: 379; python: 69; makefile: 6
file content (42 lines) | stat: -rwxr-xr-x 870 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/python3
# SPDX-License-Identifier: LGPL-2.1+

PORT = 4321

import http.server
import os
import socket
import socketserver
import sys
import time
os.chdir(sys.argv[1])

if len(sys.argv) >= 3:
    PORT = int(sys.argv[2])

def send_notify(text):

    if text is None or text == "":
        return

    e = os.getenv("NOTIFY_SOCKET")
    if e is None:
        return

    assert len(e) >= 2
    assert e[0] == '/' or e[0] == '@'

    fd = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
    fd.connect("\0" + e[1:] if e[0] == '@' else e)
    fd.send(bytes(text, 'utf-8'))

class AllowReuseAddressServer(socketserver.TCPServer):
    allow_reuse_address = True

    def server_activate(self):
        super().server_activate()
        send_notify("READY=1")

httpd = AllowReuseAddressServer(("", PORT), http.server.SimpleHTTPRequestHandler)

httpd.serve_forever()