aboutsummaryrefslogtreecommitdiff
path: root/lava_android_test/test_definitions/methanol/start_server.py
blob: 6ca96b5f15cab54b49ec12b9990a9fd4facfdc58 (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
#/usr/bin/python
# Copyright (c) 2012 Linaro

# Author: Linaro Validation Team <linaro-dev@lists.linaro.org>
#
# This file is part of LAVA Android Test.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os
import sys
import CGIHTTPServer
import BaseHTTPServer

### check parameter
if len(sys.argv) < 3:
    print 'Please spsecify the ip and directory like this:'
    print '    %s domain-or-ip directory-path url-file' % (
                                                   os.path.basename(__file__))
    sys.exit(1)

domain = sys.argv[1]
directory = sys.argv[2]
if len(sys.argv) == 4:
    url_file = sys.argv[3]
else:
    url_file = ''

## change to that directory
old_dir = os.getcwd()
os.chdir(directory)

## set the server configuration before start
cgi_handler = CGIHTTPServer.CGIHTTPRequestHandler
cgi_handler.cgi_directories.append('/cgi')
httpd = BaseHTTPServer.HTTPServer((domain, 0), cgi_handler)
url = '%s://%s:%s/' % ('http', httpd.socket.getsockname()[0],
                        httpd.socket.getsockname()[1])

## out put the url information to console and file for other script reference
print "serving at url=", url
if url_file:
    with open(url_file, 'w') as stream:
        stream.write(url)
        print 'The information of url also have been wirtten into file(%s)' % (
                                    url_file)

try:
    httpd.serve_forever()
finally:
    os.chdir(old_dir)