-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercises 13.py
More file actions
96 lines (79 loc) · 2.76 KB
/
Copy pathExercises 13.py
File metadata and controls
96 lines (79 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""
1. Implement a Flask backend service that tells whether a number received as a parameter is a prime number or not.
Use the prior prime number exercise as a starting point. For example, a GET request for number 31 is given as
: http://127.0.0.1:5000/prime_number/31. The response must be in the format of {"Number":31, "isPrime":true}.
"""
import json
from flask import Flask, Response, request
app = Flask(__name__)
@app.route('/prime_number/<number>')
def prime_number(number):
number = int (number)
divisible = True
if number > 1:
for i in range(2, number):
if number % i == 0:
divisible = False
break
else: divisible == True
response = {
"Number" : number,
"isPrime": divisible
}
return response
if __name__ == '__main__':
app.run(use_reloader=True, host='127.0.0.1', port=5000)
# http://127.0.0.1:5000/prime_number/31
"""
2. Implement a backend service that gets the ICAO code of an airport and then returns the name and location of the airport
in JSON format. The information is fetched from the airport database used on this course. For example, the GET request for
EFHK would be: http://127.0.0.1:5000/airport/EFHK. The response must be in the format
of: {"ICAO":"EFHK", "Name":"Helsinki-Vantaa Airport", "Location":"Helsinki"}.
"""
import mysql.connector
connection = mysql.connector.connect(
host='127.0.0.1',
port=3306,
database='flight_game',
user='root',
password='1234',
autocommit=True
)
app = Flask(__name__)
@app.route('/airport/<icao>')
def airport(icao):
try:
sql = "select name,municipality from airport where ident = '" + icao + "'"
db_cursor = connection.cursor()
db_cursor.execute(sql)
result = db_cursor.fetchall()
if db_cursor.rowcount > 0:
for row in result:
name = row[0]
location = row[1]
response = {
"ICAO": icao,
"Name": name,
"Location": location
}
return response
except ValueError:
response = {
"message": "Invalid number as addend",
"status": 400
}
json_response = json.dumps(response)
http_response = Response(response=json_response, status=400, mimetype="application/json")
return http_response
@app.errorhandler(404)
def page_not_found(error_code):
response = {
"message": "Invalid endpoint",
"status": 404
}
json_response = json.dumps(response)
http_response = Response(response=json_response, status=404, mimetype="application/json")
return http_response
if __name__ == '__main__':
app.run(use_reloader=True, host='127.0.0.1', port=5000)
# http://127.0.0.1:5000/airport/EFHK