- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpastebin.py
More file actions
Latest commit
108 lines (85 loc) · 2.91 KB
/
Copy pathpastebin.py
File metadata and controls
108 lines (85 loc) · 2.91 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
fromflaskimportFlask, request, session, g, redirect, url_for, abort, \
render_template, flash, send_from_directory, Response
fromrandomimportsample
fromstringimportascii_letters, digits
importwerkzeug.security
importlogging
importsqlite3
logging.basicConfig(filename='paste.log',level=logging.DEBUG)
app=Flask(__name__, static_url_path='')
defapp_start(host,port):
app.run(host=host, port=port)
DATABASE='paste.db'
defget_db():
db=getattr(g, '_database', None)
ifdbisNone:
db=g._database=sqlite3.connect(DATABASE)
returndb
@app.teardown_appcontext
defclose_connection(exception):
db=getattr(g, '_database', None)
ifdbisnotNone:
db.close()
defquery_db(query, args=(), one=False):
cur=get_db().execute(query, args)
rv=cur.fetchall()
cur.close()
return (rv[0] ifrvelseNone) ifoneelserv
definsert_db(query,args=()):
cur=get_db().execute(query, args)
get_db().commit()
cur.close()
defcheck_id(idx):
returnquery_db("Select id from pastes where id=?",[idx],True)
defmake_id():
whileTrue:
id="".join(sample((ascii_letters) *10, 10))
ifnotcheck_id(id):
returnid
defmake_password():
s=ascii_letters+digits+"!@*^$()[]+,.:~-_"
passlen=15
return"".join(sample(s,passlen ))
definsert_paste(idx,contentx,passwordx):
hashpw=werkzeug.security.generate_password_hash(passwordx)
insert_db("INSERT INTO pastes (id, content, password) VALUES(?, ?, ?)",[idx, contentx, hashpw])
defget_paste(idx):
data=query_db("Select content from pastes where id=?",[idx],True)
ifdata:
returndata[0]
else:
return"Invalid ID!"
defdelete_paste(idx,password):
data=query_db("Select password from pastes where id=?",[idx],True)
ifwerkzeug.security.check_password_hash(data[0],password) isTrue:
insert_db("DELETE FROM pastes WHERE id = ?",[idx])
return"Paste deleted!"
else:
ifnotdata:
return"Invalid paste ID."
ifdata[0] !=password:
return"Invalid password."
@app.route("/")
defshow_form():
returnapp.send_static_file('index.html')
@app.route("/process/", methods=["POST"])
defcreate_paste():
fname=make_id()
password=make_password()
text=request.form.get("text")
insert_paste(fname,text,password)
logging.info(fname)
returnrender_template('process.html', fname=fname, password=password)
@app.route("/p/<slug>/")
defshow_paste(slug):
contents=get_paste(slug)
returnrender_template('paste.html', pasteContent=contents,)
@app.route("/p/<slug>/raw")
defshow_raw_paste(slug):
contents=get_paste(slug)
returnResponse(contents, mimetype='text/plain')
@app.route("/p/<slug>/delete/<slug2>")
defremove_paste(slug,slug2):
result=delete_paste(slug,slug2)
returnresult
app_start('0.0.0.0',6060)