forked from pahaz/homework-simple-python-web-application
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
35 lines (29 loc) · 1016 Bytes
/
main.py
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
__author__ = 'Alexey'
from bottle import route, run, template, request
from MyModel import AddressModel
import httplib
import urllib2
ADDR_DB = 'addresses.db'
@route('/')
def index():
return template('index', content=False)
@route('/shorten', method='POST')
def shorten():
address = request.forms.get('address')
if model.exist_address(address):
return template('index', content=address+': localhost:8051/'+model.get_id(address))
else:
try:
urllib2.urlopen('http://'+address)
addrid = model.new(address)
return template('index', content=address+': localhost:8051/'+addrid)
except:
return template('index', content="Can't accept this url")
@route('/<num>')
def goto(num):
if model.exist_id(num):
return '<meta http-equiv="refresh" content="0; http://' + model.get(num) + '">'
else:
return template('index', content='Incorrrect link')
model = AddressModel(ADDR_DB)
run(host = 'localhost', port = 8051)