Sharedsupport / 2015-09-02-090517-webserver / main.pyOpen in CoCalc
Authors: Harald Schilly, ℏal Snyder, William A. Stein
License: GNU General Public License v3.0
Description: Examples for support purposes.
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
# This runs a demo-webservice inside SMC.
4
# Run by typing
5
# sage -python main.py
6
7
port = 12345
8
9
def get_eth0ip():
10
import subprocess as sp
11
ifconfig = sp.check_output(["ifconfig", "eth0" ])
12
eth0ip = ifconfig.split("\n")[1].split(":")[1].split(" ")[0]
13
print "eth0 IP address is '%s'" % eth0ip
14
return eth0ip
15
16
def projectid():
17
import json
18
import os
19
info = json.load(open(os.path.join(os.environ['HOME'], ".sagemathcloud", "info.json"), 'r'))
20
return info['project_id']
21
22
base_url = "/%s/port/%s/" % (projectid(), port)
23
24
from flask import Flask
25
app = Flask(__name__)
26
27
@app.route(base_url)
28
def hello_world():
29
from datetime import datetime
30
return 'Hello World!\nThe current time is %s' % datetime.utcnow()
31
32
if __name__ == "__main__":
33
try:
34
print("Try to open https://cloud.sagemath.com" + base_url)
35
app.run(host = get_eth0ip(), port = port)
36
import sys; sys.exit(0)
37
except Exception as e:
38
print "... failed, try another port (change the port= line in the script) \n%s" % e