回答編集履歴
4
シェバン行が重複していたので削除!
answer
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
```Python
|
|
4
4
|
# -*- coding: utf8 -*-
|
|
5
|
-
# -*- coding: utf8 -*-
|
|
6
5
|
import os
|
|
7
6
|
from http.server import HTTPServer, SimpleHTTPRequestHandler
|
|
8
7
|
from socketserver import ThreadingMixIn
|
3
簡易的なIPアドレスによる制限を追加
answer
CHANGED
|
@@ -6,20 +6,31 @@
|
|
|
6
6
|
import os
|
|
7
7
|
from http.server import HTTPServer, SimpleHTTPRequestHandler
|
|
8
8
|
from socketserver import ThreadingMixIn
|
|
9
|
+
from ipaddress import ip_address
|
|
9
10
|
import html
|
|
10
11
|
|
|
11
12
|
|
|
12
13
|
class MyHandler(SimpleHTTPRequestHandler):
|
|
13
|
-
def to_content(self) ->str:
|
|
14
|
+
def to_content(self) -> str:
|
|
14
15
|
body = f"time:{html.escape(self.date_time_string())}<br>dir:{html.escape(os.getcwd())}"
|
|
15
16
|
content = f"<html><head></head><body>{body}</body></html>"
|
|
16
17
|
return content
|
|
17
18
|
|
|
18
19
|
def do_GET(self):
|
|
20
|
+
req_ip = ip_address(self.client_address[0])
|
|
21
|
+
# IPアドレスによる簡易的なアクセス制限
|
|
22
|
+
# ループバック,ローカルアドレス以外はステータスコード:403を返す
|
|
23
|
+
is_accepted = any([req_ip.is_loopback, req_ip.is_private])
|
|
24
|
+
if is_accepted:
|
|
19
|
-
|
|
25
|
+
self.send_response(200)
|
|
20
|
-
|
|
26
|
+
self.end_headers()
|
|
21
|
-
|
|
27
|
+
self.wfile.write(self.to_content().encode('utf-8'))
|
|
22
|
-
|
|
28
|
+
self.wfile.write(b'\n')
|
|
29
|
+
return
|
|
30
|
+
else:
|
|
31
|
+
self.send_response(403)
|
|
32
|
+
self.end_headers()
|
|
33
|
+
return
|
|
23
34
|
|
|
24
35
|
|
|
25
36
|
class ThreadingHTTPServer(ThreadingMixIn, HTTPServer):
|
|
@@ -30,7 +41,7 @@
|
|
|
30
41
|
PORT = 8000
|
|
31
42
|
print(os.getcwd())
|
|
32
43
|
print(os.path.abspath(__file__))
|
|
33
|
-
with
|
|
44
|
+
with ThreadingHTTPServer(("", PORT), MyHandler) as httpd:
|
|
34
45
|
print("serving at port", PORT)
|
|
35
46
|
httpd.serve_forever()
|
|
36
47
|
|
|
@@ -38,8 +49,4 @@
|
|
|
38
49
|
if __name__ == '__main__':
|
|
39
50
|
main()
|
|
40
51
|
|
|
41
|
-
```
|
|
52
|
+
```
|
|
42
|
-
|
|
43
|
-
◇お願い。
|
|
44
|
-
外部からローカルパスを公開するのはセキュリティ的にアレではと思いつつ。
|
|
45
|
-
アクセス制限とかを絶対に掛けてくださいな。
|
2
html#escapeをしていなかった件について
answer
CHANGED
|
@@ -2,27 +2,23 @@
|
|
|
2
2
|
|
|
3
3
|
```Python
|
|
4
4
|
# -*- coding: utf8 -*-
|
|
5
|
+
# -*- coding: utf8 -*-
|
|
5
6
|
import os
|
|
6
7
|
from http.server import HTTPServer, SimpleHTTPRequestHandler
|
|
7
8
|
from socketserver import ThreadingMixIn
|
|
8
|
-
|
|
9
|
+
import html
|
|
9
10
|
|
|
10
11
|
|
|
11
12
|
class MyHandler(SimpleHTTPRequestHandler):
|
|
13
|
+
def to_content(self) ->str:
|
|
14
|
+
body = f"time:{html.escape(self.date_time_string())}<br>dir:{html.escape(os.getcwd())}"
|
|
15
|
+
content = f"<html><head></head><body>{body}</body></html>"
|
|
16
|
+
return content
|
|
12
17
|
|
|
13
|
-
def to_html(self):
|
|
14
|
-
t = Template('<br>time:$time<br>cwd:$cwd')\
|
|
15
|
-
.substitute(time=self.date_time_string(), cwd=os.getcwd())
|
|
16
|
-
body = """<html><head></head><body>
|
|
17
|
-
{0}
|
|
18
|
-
</body></html>
|
|
19
|
-
""".format(t)
|
|
20
|
-
return body
|
|
21
|
-
|
|
22
18
|
def do_GET(self):
|
|
23
19
|
self.send_response(200)
|
|
24
20
|
self.end_headers()
|
|
25
|
-
self.wfile.write(self.
|
|
21
|
+
self.wfile.write(self.to_content().encode('utf-8'))
|
|
26
22
|
self.wfile.write(b'\n')
|
|
27
23
|
|
|
28
24
|
|
1
追記
answer
CHANGED
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
return body
|
|
21
21
|
|
|
22
22
|
def do_GET(self):
|
|
23
|
-
self.send_response(
|
|
23
|
+
self.send_response(200)
|
|
24
24
|
self.end_headers()
|
|
25
25
|
self.wfile.write(self.to_html().encode('utf-8'))
|
|
26
26
|
self.wfile.write(b'\n')
|