CoCalc Public Filesbrowser
Views : 79
Compute Environment: Ubuntu 18.04 (Deprecated)
1#!/usr/bin/env python
2
3######################################################################
5# 2-clause BSD:
6# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
10# The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of the SageMath Project.
11######################################################################
12
13import json, os, random, BaseHTTPServer
14from SimpleHTTPServer import SimpleHTTPRequestHandler
15
17project_id = info['project_id']
18ip = info['location']['host']
19
20def random_port():
21    # get an available port; a race condition is possible, but very, very unlikely.
22    while True:
23        port = random.randint(1025,65536)
24        a = os.popen("netstat -ano|grep %s|grep LISTEN"%port).read()
25        if len(a) < 5:
26            return port
27
28port = random_port()
29base = "%s/%s/port/%s/"%(info['base_url'], project_id, port)
30
31print "*"*80 + '\n'
32print "  The HTTP server is running at \n"
33print "      https://cloud.sagemath.com%s\n"%base
34print "  All collaborators on this project may access the server at the"
35print "  above SSL-encrypted URL, but nobody else can access it."
36print '\n\n' + "*"*80 + '\n\n'
37
38class MyHandler(SimpleHTTPRequestHandler):
39
40    def do_GET(self):
41        if self.path.startswith(base):
42            self.path = self.path[len(base)-1:]
43            print "Note: path rewritten to:", self.path
44        return SimpleHTTPRequestHandler.do_GET(self)
45
46MyHandler.protocol_version = "HTTP/1.0"
47httpd = BaseHTTPServer.HTTPServer((ip, port), MyHandler)
48sa = httpd.socket.getsockname()
49print "Serving HTTP on", sa[0], "port", sa[1], "base", base
50httpd.serve_forever()