-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathilksitem.py
More file actions
120 lines (100 loc) · 3.71 KB
/
ilksitem.py
File metadata and controls
120 lines (100 loc) · 3.71 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
"""
db.json dosyası içinde bir dict vardır (ya da json object),
o dict içindeki iki key kullanici ve notlar da birer dict'e key-value bağı ile bağlıdır
kullanici alt dict'inde key kullanıcı adı, value ise sha256 ile şifrelenmiş parolasıdır
notlar alt dict'inde key kullanıcı adı, value ise kullanıcının girdiği nottur.
"""
from flask import Flask, render_template, request, Response
import werkzeug.exceptions
from flask_httpauth import HTTPBasicAuth
from hashlib import sha256
from datetime import datetime
from random import randint
from os import path
import json
app = Flask("ilksitem")
auth = HTTPBasicAuth()
@auth.get_password
def get_pw(username):
if path.exists('db.json'):
with open('db.json') as f:
try:
veriler = json.load(f)["kullanici"]
except ValueError as e:
print("database bozuk: " + str(e.args))
return None
except KeyError:
print("tablo bulunamadı")
return None
try:
return veriler[username]
except KeyError:
print("kullanıcı bulunamadı: " + username)
return None
else:
print("database bulunamadı")
return None
@auth.hash_password
def hash_pw(password):
return sha256(password.encode()).hexdigest()
@app.route("/xml_deneme")
def xmldeneme():
import xml.etree.ElementTree as ET
html = ET.Element("html")
head = ET.SubElement(html, "head")
title = ET.SubElement(head, "title")
title.text = "Test"
body = ET.SubElement(html,"body")
body.text = "abcd"
return Response(ET.tostring(html), mimetype="application/xml")
@app.route("/json_deneme")
def jsondeneme():
try:
with open('db.json') as f:
veriler = json.load(f)
except (FileNotFoundError, KeyError, ValueError) as e:
print("HATA: " + str(type(e)) + str(e.args))
return werkzeug.exceptions.InternalServerError("Veritabanı Bulunamadı")
return Response(json.dumps(
{
"kullanicilar": list(veriler["kullanici"].keys()),
"not_girmis_kullanicilar":list(
map(lambda tupl: tupl[0],
filter(lambda tupl:tupl[1]!= "",
veriler["notlar"].items()
)
)
)
}
), mimetype="application/json")
@app.route("/", methods=['GET', 'POST'])
@auth.login_required
def anasayfa():
notlar = ""
try:
with open('db.json') as f:
veriler = json.load(f)["notlar"]
notlar = veriler[auth.username()]
except (FileNotFoundError, KeyError, ValueError) as e:
print("HATA: " + str(type(e)) + str(e.args))
if request.method == "POST" and "notlar" in request.values:
try:
with open('db.json') as f:
veriler = json.load(f)
notlar = veriler["notlar"][auth.username()] = request.values["notlar"]
with open('db.json', "w") as f:
json.dump(veriler, f, indent=2)
except (FileNotFoundError, KeyError, ValueError) as e:
print("HATA: " + str(type(e)) + str(e.args))
veriler = {
'zaman': datetime.now(),
'kazanan_kupon': [],
'notlar': notlar,
}
for i in range(6):
rakam = randint(1, 49)
veriler['kazanan_kupon'].append(rakam)
return render_template(
"anasayfa.html", **veriler)
if __name__ == '__main__':
app.run(port=8080, debug=True)