-
Notifications
You must be signed in to change notification settings - Fork 0
/
wec3-get-page.py
67 lines (55 loc) · 1.96 KB
/
wec3-get-page.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
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
#!/usr/bin/python3.6
# -*- coding: utf-8 -*-
from http.server import HTTPServer, SimpleHTTPRequestHandler, test
from fileCls import File
html = """<html>
<head><title>Title goes here.</title></head>
<body>
<p>This is a test.</p>
</body></html>
"""
fileHtml = File ('b/test-be.html')
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 do_GET (self):
self.send_response (200)
self.end_headers()
self.writeBody (html)
def do_POST (self):
self.send_response (200)
self.end_headers()
postBody = self.readBody()
pBody = postBody.find ('\n\n')
fileHtml.title = postBody [:pBody]
if not fileHtml.title or len (fileHtml.title) <3: fileHtml.title = 'tmp'
fileHtml.text = postBody [pBody +2:]
fileHtml.write()
self.writeBody ('ok '+ fileHtml.title)
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()
pour utiliser le script comme back-end dans un fichier js
const url = 'http://localhost:1407/test.py';
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){ if (this.readyState ==4) console.log (this.responseText); };
xhttp.open ('GET', url, true);
xhttp.send();
"""