Skip to content

Commit 67a2446

Browse files
committed
webservice demo (currently not working)
1 parent 8780e57 commit 67a2446

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

webservice/Readme.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Webservice
2+
----------
3+
4+
This is a small demo how a web-service can be started inside SMC.
5+
See `main.py` for more details.
6+
7+
Start it in the terminal via:
8+
9+
`$ sage -python main.py`
10+

webservice/main.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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

webservice/run.term

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
41e9336f-ca0e-457a-a0cc-9b4e8841c6be

0 commit comments

Comments
 (0)