@@ -1503,14 +1503,14 @@ Web
1503
1503
---
1504
1504
``` python
1505
1505
# $ pip3 install bottle
1506
- import bottle
1507
- from urllib.parse import unquote
1506
+ from bottle import run, route, post, template, request, response
1507
+ import json
1508
1508
```
1509
1509
1510
1510
### Run
1511
1511
``` python
1512
- bottle. run(host = ' localhost' , port = 8080 )
1513
- bottle. run(host = ' 0.0.0.0' , port = 80 , server = ' cherrypy' )
1512
+ run(host = ' localhost' , port = 8080 )
1513
+ run(host = ' 0.0.0.0' , port = 80 , server = ' cherrypy' )
1514
1514
```
1515
1515
1516
1516
### Static Request
@@ -1524,27 +1524,29 @@ def send_image(image):
1524
1524
``` python
1525
1525
@route (' /<sport>' )
1526
1526
def send_page (sport ):
1527
- sport = unquote(sport).lower()
1528
- page = read_file(sport)
1529
- return template(page)
1527
+ return template(' <h1>{{ title}} </h1>' , title = sport)
1530
1528
```
1531
1529
1532
1530
### REST Request
1533
1531
``` python
1534
1532
@post (' /odds/<sport>' )
1535
1533
def odds_handler (sport ):
1536
- team = bottle.request.forms.get(' team' )
1537
- team = unquote(team).lower()
1538
-
1539
- db = sqlite3.connect(< db_path> )
1540
- home_odds, away_odds = get_odds(db, sport, team)
1541
- db.close()
1542
-
1534
+ team = request.forms.get(' team' )
1535
+ home_odds, away_odds = 2.44 , 3.29
1543
1536
response.headers[' Content-Type' ] = ' application/json'
1544
1537
response.headers[' Cache-Control' ] = ' no-cache'
1545
1538
return json.dumps([home_odds, away_odds])
1546
1539
```
1547
1540
1541
+ #### Test:
1542
+ ``` python
1543
+ # $ pip3 install requests
1544
+ >> > import requests
1545
+ >> > r = requests.post(' http://localhost:8080/odds/soccer' , data = {' team' : ' arsenal' })
1546
+ >> > r.json()
1547
+ [2.44 , 3.29 ]
1548
+ ```
1549
+
1548
1550
1549
1551
Profile
1550
1552
-------
0 commit comments