CoCalc Public FilesbrowserOpen with one click!
Authors: Gihan Marasingha, Harald Schilly, ℏal Snyder, William A. Stein
Views : 79
Compute Environment: Ubuntu 18.04 (Deprecated)
1
#!/usr/bin/env python
2
3
######################################################################
4
# Copyright (c) 2013, William Stein, Ondrej Certik, All rights reserved.
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
13
import json, os, random, BaseHTTPServer
14
from SimpleHTTPServer import SimpleHTTPRequestHandler
15
16
info = json.loads(open(os.path.join(os.environ['SAGEMATHCLOUD'], 'info.json')).read())
17
project_id = info['project_id']
18
ip = info['location']['host']
19
20
def 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
28
port = random_port()
29
base = "%s/%s/port/%s/"%(info['base_url'], project_id, port)
30
31
print "*"*80 + '\n'
32
print " The HTTP server is running at \n"
33
print " https://cloud.sagemath.com%s\n"%base
34
print " All collaborators on this project may access the server at the"
35
print " above SSL-encrypted URL, but nobody else can access it."
36
print '\n\n' + "*"*80 + '\n\n'
37
38
class 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
46
MyHandler.protocol_version = "HTTP/1.0"
47
httpd = BaseHTTPServer.HTTPServer((ip, port), MyHandler)
48
sa = httpd.socket.getsockname()
49
print "Serving HTTP on", sa[0], "port", sa[1], "base", base
50
httpd.serve_forever()