knative-report-functions/isprime-python/app.py

29 lines
606 B
Python
Raw Normal View History

2019-10-08 18:16:16 +02:00
import os
from flask import Flask
app = Flask(__name__)
2019-10-18 22:55:26 +02:00
def safe_cast(val, to_type, default=107107):
try:
return to_type(val)
except (ValueError, TypeError):
return default
2019-10-10 15:34:57 +02:00
2019-10-08 18:16:16 +02:00
@app.route('/')
2019-10-08 18:42:36 +02:00
def isprime():
2019-10-18 22:55:26 +02:00
num = safe_cast(os.environ.get('TARGET', 107107),int)
2019-10-10 15:34:57 +02:00
if num > 1:
for i in range(2, num):
if (num % i) == 0:
2020-01-13 13:08:16 +01:00
return str(i)
2019-10-10 15:34:57 +02:00
else:
return "not prime"
else:
print(num, "is not a prime number")
2019-10-08 18:16:16 +02:00
if __name__ == "__main__":
2019-11-10 20:44:56 +01:00
app.run(debug=True,host='0.0.0.0',port=int(os.environ.get('PORT', 8080)))