-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.py
More file actions
128 lines (115 loc) · 3.81 KB
/
backend.py
File metadata and controls
128 lines (115 loc) · 3.81 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
126
127
128
#!/usr/bin/python3.9
# -*- coding: utf-8 -*-
""" ouvrir des fichiers via un navigateur
dépendences js:
text.js
fileBackend.js
"""
import json
from http.server import HTTPServer, SimpleHTTPRequestHandler, test
import textFct
import fileCls
htmlTest = """<html>
<head><title>serveur python</title></head>
<body>
<p>serveur python pour ouvrir des fichiers</p>
</body></html>
"""
fileArticle = 'b/ .html'
def findPath (postBody):
path =""
if 'path' in postBody.keys(): path = postBody['path']
else: path = fileArticle.replace ('\t', postBody['title'])
return path
class BackEndCors (SimpleHTTPRequestHandler):
def end_headers (self):
self.send_header ('Access-Control-Allow-Origin', '*')
self.send_header ('Access-Control-Allow-Methods', '*')
self.send_header ('Access-Control-Allow-Headers', '*')
SimpleHTTPRequestHandler.end_headers (self)
def readBody (self):
# rfile.read renvoie un texte en byte, il faut le transformer en string
bodyLen = int (self.headers.get ('Content-Length'))
bodyText =""
if bodyLen >0: bodyText = self.rfile.read (bodyLen).decode('utf-8')
return bodyText
def writeBody (self, text):
# wfile.write prend un texte en bytes comme argument, il faut parser les strings
self.wfile.write (bytes (text, 'utf-8'))
# self.wfile.close()
def readArticle (self, postBody):
fileToRead = fileCls.Article (postBody['file'])
fileToRead.read()
if fileToRead.path[-5:] == '.html': fileToRead.text = textFct.cleanHtml (fileToRead.text)
else: fileToRead.text = textFct.cleanText (fileToRead.text)
sendBody ={
'title': fileToRead.title,
'text': fileToRead.text,
'path': fileToRead.path,
'author': fileToRead.author,
'link': fileToRead.link,
'authLink': fileToRead.authLink,
'subject': fileToRead.subject
}
return sendBody
def readFile (self, postBody):
fileToRead = fileCls.File (postBody['file'])
fileToRead.read()
if fileToRead.path[-5:] == '.html': fileToRead.text = textFct.cleanHtml (fileToRead.text)
elif fileToRead.path[-4:] == '.css': fileToRead.text = textFct.cleanCss (fileToRead.text)
else: fileToRead.text = textFct.cleanText (fileToRead.text)
sendBody ={
'title': fileToRead.title,
'text': fileToRead.text,
'path': fileToRead.path
}
return sendBody
def writeFile (self, postBody):
path = findPath (postBody)
fileToRead = fileCls.Article (path)
fileToRead.link = postBody['link']
fileToRead.text = postBody['text']
fileToRead.author ='o'
fileToRead.authLink ='o'
fileToRead.subject ='o'
fileToRead.write()
def writeArticle (self, postBody):
path = findPath (postBody)
fileToRead = fileCls.Article (path)
fileToRead.link = postBody['link']
fileToRead.text = postBody['text']
fileToRead.author = postBody['author']
fileToRead.authLink = postBody['authLink']
fileToRead.subject = postBody['subject']
fileToRead.write()
"""
def do_GET (self):
self.send_response (200)
self.end_headers()
self.writeBody (htmlTest)
"""
def do_POST (self):
""" lire un fichier
postBody = { text: 'mon long texte à écrire', path: 'path/to/myfile.html' }
"""
self.send_response (200)
self.end_headers()
bodyTmp = self.readBody()
postBody = json.loads (bodyTmp)
sendBody ={}
if 'text' in postBody.keys():
if 'author' in postBody.keys(): self.writeArticle (postBody)
else: self.writeFile (postBody)
sendBody['reponse'] = 'fichier écrit'
elif 'type' in postBody.keys() and postBody['type'] == 'text': sendBody = self.readFile (postBody)
else: sendBody = self.readArticle (postBody)
sendJson = json.dumps (sendBody)
self.writeBody (sendJson)
if __name__ == '__main__':
test (BackEndCors, HTTPServer, port=1407)
"""
url correspondant à index.html
http://localhost:1407/
si je rajoute une fonction do_GET à ma classe, le html du fichier est écrasé.
il faut générer du nouveau html dynamiquement grâce à self.wfile()
"""