Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
1999 views
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
# This runs a demo-webservice inside SMC
4
5
def get_tun0ip():
6
import subprocess as sp
7
ifconfig = sp.check_output(["ifconfig", "tun0" ])
8
tun0ip = ifconfig.split("\n")[1].split(":")[1].split(" ")[0]
9
print "tun0 IP address is '%s'" % tun0ip
10
return tun0ip
11
12
def projectid():
13
import json
14
import os
15
info = json.load(open(os.path.join("..", ".sagemathcloud", "info.json"), 'r'))
16
return info['project_id']
17
18
from flask import Flask
19
app = Flask(__name__)
20
21
@app.route('/')
22
def hello_world():
23
from datetime import datetime
24
return 'Hello World!\nThe current time is %s' % datetime.utcnow()
25
26
if __name__ == "__main__":
27
for i in range(10):
28
import random
29
port = random.randint(2000, 32000)
30
try:
31
print "Tying to open https://cloud.sagemath.org/%s/port/%s/" % (projectid(), port)
32
#app.run(host = get_tun0ip(), port = port)
33
app.run(host = "0.0.0.0", port = port)
34
import sys; sys.exit(0)
35
except Exception as e:
36
print "... failed, trying other port\n%s" % e
37
38