barrabrasil.py
4.7 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from flask import Flask, url_for, render_template, request, Response, make_response
from config import Config
import hashlib, zlib, datetime, os
# Criar Key e certificado
# openssl genrsa 1024 > ssl.key
# openssl req -new -x509 -nodes -sha1 -days 365 -key ssl.key > ssl.cert
#from OpenSSL import SSL
#ctx = SSL.Context(SSL.SSLv23_METHOD)
#ctx.use_privatekey_file('ssl.key')
#ctx.use_certificate_file('ssl.cert')
app = Flask(__name__,static_url_path="/fonts", static_folder="fonts")
if not app.debug:
import logging
from logging.handlers import SMTPHandler
f = app.open_resource('config')
cfg = Config(f)
mail_handler = SMTPHandler(cfg.server, cfg.email, cfg.ADMINS, cfg.subject)
mail_handler.setLevel(logging.ERROR)
app.logger.addHandler(mail_handler)
@app.route('/')
def pagina_teste():
try:
with app.open_resource('templates/exemplo.html') as f:
conteudo = f.read().decode('utf-8')
resposta = make_response(conteudo)
resposta.headers['Content-type'] = 'text/html; charset=utf-8'
return resposta
except IOError:
return make_response("<h1>403 Forbidden</h1>", 403)
@app.route('/exemplo_antiga.html')
def pagina_teste_antiga():
try:
with app.open_resource('templates/exemplo_antiga.html') as f:
conteudo = f.read().decode('utf-8')
resposta = make_response(conteudo)
resposta.headers['Content-type'] = 'text/html; charset=utf-8'
return resposta
except IOError:
return make_response("<h1>403 Forbidden</h1>", 403)
@app.route('/imagens/vlibras.gif')
def vlibras_teste():
try:
with app.open_resource('static/vlibras.gif') as f:
conteudo = f.read()
resposta = make_response(conteudo)
resposta.headers['Content-type'] = 'image/gif'
return resposta
except IOError:
return make_response("<h1>403 Forbidden</h1>", 403)
@app.route('/barra.js')
def barra2_0():
with app.open_resource('templates/'+os.getenv('PROFILE', 'default')+'/barra-brasil.js') as f:
conteudo = f.read().decode('utf-8')
etag = hashlib.sha1(conteudo.encode('utf-8')).hexdigest()
accept_encoding = request.headers.get('Accept-Encoding', '')
if request.if_none_match and \
etag in request.if_none_match:
resposta = Response(status=304)
if accept_encoding and \
'gzip' in accept_encoding:
conteudo = zlib.compress(conteudo.encode('utf8'))
resposta.headers['Content-Encoding'] = 'gzip'
else: # nao esta em cache do navegador
resposta = make_response(conteudo)
resposta.set_etag(etag)
resposta.headers['Content-type'] = 'application/x-javascript'
resposta.headers['Cache-control'] = 'public, max-age: 31536000' #1 ano
resposta.headers['Last-Modified'] = datetime.datetime.now()
return resposta
@app.route('/barra_2.0.js')
def barra():
with app.open_resource('templates/default/barra-brasil.js') as f:
conteudo = f.read().decode('utf-8')
etag = hashlib.sha1(conteudo.encode('utf-8')).hexdigest()
accept_encoding = request.headers.get('Accept-Encoding', '')
if request.if_none_match and \
etag in request.if_none_match:
resposta = Response(status=304)
if accept_encoding and \
'gzip' in accept_encoding:
conteudo = zlib.compress(conteudo.encode('utf8'))
resposta.headers['Content-Encoding'] = 'gzip'
else: # nao esta em cache do navegador
resposta = make_response(conteudo)
resposta.set_etag(etag)
resposta.headers['Content-type'] = 'application/x-javascript'
resposta.headers['Cache-control'] = 'public, max-age: 31536000' #1 ano
resposta.headers['Last-Modified'] = datetime.datetime.now()
return resposta
@app.route('/static/jquery.js')
def jquery():
with app.open_resource('static/jquery-2.1.3.min.js') as f:
conteudo = f.read().decode('utf-8')
etag = hashlib.sha1(conteudo.encode('utf-8')).hexdigest()
accept_encoding = request.headers.get('Accept-Encoding', '')
if request.if_none_match and \
etag in request.if_none_match:
resposta = Response(status=304)
if accept_encoding and \
'gzip' in accept_encoding:
conteudo = zlib.compress(conteudo.encode('utf8'))
resposta.headers['Content-Encoding'] = 'gzip'
else: # nao esta em cache do navegador
resposta = make_response(conteudo)
resposta.set_etag(etag)
resposta.headers['Content-type'] = 'application/x-javascript'
resposta.headers['Cache-control'] = 'public, max-age: 31536000' #1 ano
resposta.headers['Last-Modified'] = datetime.datetime.now()
return resposta
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
#webbrowser.open("https://127.0.0.1:5000/",new=2)
#app.run(debug=False,ssl_context=ctx)