summaryrefslogtreecommitdiffstats
path: root/testing/awsy/awsy/webservers.py
blob: ca278db706a1415e6ef22fac37449b183ccbfcfd (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.


# mozhttpd web server.

import argparse
import os
import socket

import mozhttpd

# directory of this file
here = os.path.dirname(os.path.realpath(__file__))


class WebServers(object):
    def __init__(self, host, port, docroot, count):
        self.host = host
        self.port = port
        self.docroot = docroot
        self.count = count
        self.servers = []

    def start(self):
        self.stop()
        self.servers = []
        port = self.port
        num_errors = 0
        while len(self.servers) < self.count:
            self.servers.append(
                mozhttpd.MozHttpd(host=self.host, port=port, docroot=self.docroot)
            )
            try:
                self.servers[-1].start()
            except socket.error as error:
                if isinstance(error, socket.error):
                    if error.errno == 98:
                        print("port {} is in use.".format(port))
                    else:
                        print("port {} error {}".format(port, error))
                elif isinstance(error, str):
                    print("port {} error {}".format(port, error))
                self.servers.pop()
                num_errors += 1
            except Exception as error:
                print("port {} error {}".format(port, error))
                self.servers.pop()
                num_errors += 1

            if num_errors > 15:
                raise Exception("Too many errors in webservers.py")
            port += 1

    def stop(self):
        while len(self.servers) > 0:
            server = self.servers.pop()
            server.stop()


def main():
    parser = argparse.ArgumentParser(
        description="Start mozhttpd servers for use by areweslimyet."
    )

    parser.add_argument(
        "--port",
        type=int,
        default=8001,
        help="Starting port. Defaults to 8001. Web servers will be "
        "created for each port from the starting port to starting port "
        "+ count - 1.",
    )
    parser.add_argument(
        "--count",
        type=int,
        default=100,
        help="Number of web servers to start. Defaults to 100.",
    )
    parser.add_argument(
        "--host",
        type=str,
        default="localhost",
        help="Name of webserver host. Defaults to localhost.",
    )

    args = parser.parse_args()
    web_servers = WebServers(args.host, args.port, "%s/html" % here, args.count)
    web_servers.start()


if __name__ == "__main__":
    main()