|
@ -1503,14 +1503,14 @@ Web |
|
|
--- |
|
|
--- |
|
|
```python |
|
|
```python |
|
|
# $ pip3 install bottle |
|
|
# $ pip3 install bottle |
|
|
import bottle |
|
|
|
|
|
from urllib.parse import unquote |
|
|
|
|
|
|
|
|
from bottle import run, route, post, template, request, response |
|
|
|
|
|
import json |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
### Run |
|
|
### Run |
|
|
```python |
|
|
```python |
|
|
bottle.run(host='localhost', port=8080) |
|
|
|
|
|
bottle.run(host='0.0.0.0', port=80, server='cherrypy') |
|
|
|
|
|
|
|
|
run(host='localhost', port=8080) |
|
|
|
|
|
run(host='0.0.0.0', port=80, server='cherrypy') |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
### Static Request |
|
|
### Static Request |
|
@ -1524,27 +1524,29 @@ def send_image(image): |
|
|
```python |
|
|
```python |
|
|
@route('/<sport>') |
|
|
@route('/<sport>') |
|
|
def send_page(sport): |
|
|
def send_page(sport): |
|
|
sport = unquote(sport).lower() |
|
|
|
|
|
page = read_file(sport) |
|
|
|
|
|
return template(page) |
|
|
|
|
|
|
|
|
return template('<h1>{{title}}</h1>', title=sport) |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
### REST Request |
|
|
### REST Request |
|
|
```python |
|
|
```python |
|
|
@post('/odds/<sport>') |
|
|
@post('/odds/<sport>') |
|
|
def odds_handler(sport): |
|
|
def odds_handler(sport): |
|
|
team = bottle.request.forms.get('team') |
|
|
|
|
|
team = unquote(team).lower() |
|
|
|
|
|
|
|
|
|
|
|
db = sqlite3.connect(<db_path>) |
|
|
|
|
|
home_odds, away_odds = get_odds(db, sport, team) |
|
|
|
|
|
db.close() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
team = request.forms.get('team') |
|
|
|
|
|
home_odds, away_odds = 2.44, 3.29 |
|
|
response.headers['Content-Type'] = 'application/json' |
|
|
response.headers['Content-Type'] = 'application/json' |
|
|
response.headers['Cache-Control'] = 'no-cache' |
|
|
response.headers['Cache-Control'] = 'no-cache' |
|
|
return json.dumps([home_odds, away_odds]) |
|
|
return json.dumps([home_odds, away_odds]) |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
#### Test: |
|
|
|
|
|
```python |
|
|
|
|
|
# $ pip3 install requests |
|
|
|
|
|
>>> import requests |
|
|
|
|
|
>>> r = requests.post('http://localhost:8080/odds/soccer', data={'team': 'arsenal'}) |
|
|
|
|
|
>>> r.json() |
|
|
|
|
|
[2.44, 3.29] |
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Profile |
|
|
Profile |
|
|
------- |
|
|
------- |
|
|