Add support for connection tracking.
[cascardo/ovs.git] / tests / test-l7.py
1 # Copyright (c) 2015 Nicira, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import argparse
16 import socket
17
18 from BaseHTTPServer import HTTPServer
19 from SimpleHTTPServer import SimpleHTTPRequestHandler
20 from SocketServer import TCPServer
21
22
23 class TCPServerV6(HTTPServer):
24     address_family = socket.AF_INET6
25
26
27 def get_ftpd():
28     try:
29         from pyftpdlib.authorizers import DummyAuthorizer
30         from pyftpdlib.handlers import FTPHandler
31         from pyftpdlib.servers import FTPServer
32
33         class OVSFTPHandler(FTPHandler):
34             authorizer = DummyAuthorizer()
35             authorizer.add_anonymous("/tmp")
36         server = [FTPServer, OVSFTPHandler, 21]
37     except ImportError:
38         server = None
39         pass
40     return server
41
42
43 def main():
44     SERVERS = {
45         'http':  [TCPServer,   SimpleHTTPRequestHandler, 80],
46         'http6': [TCPServerV6, SimpleHTTPRequestHandler, 80],
47     }
48
49     ftpd = get_ftpd()
50     if ftpd is not None:
51         SERVERS['ftp'] = ftpd
52
53     protocols = [srv for srv in SERVERS]
54     parser = argparse.ArgumentParser(
55             description='Run basic application servers.')
56     parser.add_argument('proto', default='http', nargs='?',
57             help='protocol to serve (%s)' % protocols)
58     args = parser.parse_args()
59
60     if args.proto not in SERVERS:
61         parser.print_help()
62         exit(1)
63
64     constructor = SERVERS[args.proto][0]
65     handler = SERVERS[args.proto][1]
66     port = SERVERS[args.proto][2]
67     srv = constructor(('', port), handler)
68     srv.serve_forever()
69
70
71 if __name__ == '__main__':
72     main()