Skip to content

Commit 43f252b

Browse files
committed
Web
1 parent b021c12 commit 43f252b

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

README.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,14 +1503,14 @@ Web
15031503
---
15041504
```python
15051505
# $ 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
15081508
```
15091509

15101510
### Run
15111511
```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')
15141514
```
15151515

15161516
### Static Request
@@ -1524,27 +1524,29 @@ def send_image(image):
15241524
```python
15251525
@route('/<sport>')
15261526
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)
15301528
```
15311529

15321530
### REST Request
15331531
```python
15341532
@post('/odds/<sport>')
15351533
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
15431536
response.headers['Content-Type'] = 'application/json'
15441537
response.headers['Cache-Control'] = 'no-cache'
15451538
return json.dumps([home_odds, away_odds])
15461539
```
15471540

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+
15481550

15491551
Profile
15501552
-------

0 commit comments

Comments
 (0)