回答編集履歴

4

シェバン行が重複していたので削除!

2018/04/14 11:47

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -3,8 +3,6 @@
3
3
 
4
4
 
5
5
  ```Python
6
-
7
- # -*- coding: utf8 -*-
8
6
 
9
7
  # -*- coding: utf8 -*-
10
8
 

3

簡易的なIPアドレスによる制限を追加

2018/04/14 11:47

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -14,6 +14,8 @@
14
14
 
15
15
  from socketserver import ThreadingMixIn
16
16
 
17
+ from ipaddress import ip_address
18
+
17
19
  import html
18
20
 
19
21
 
@@ -22,7 +24,7 @@
22
24
 
23
25
  class MyHandler(SimpleHTTPRequestHandler):
24
26
 
25
- def to_content(self) ->str:
27
+ def to_content(self) -> str:
26
28
 
27
29
  body = f"time:{html.escape(self.date_time_string())}<br>dir:{html.escape(os.getcwd())}"
28
30
 
@@ -34,13 +36,33 @@
34
36
 
35
37
  def do_GET(self):
36
38
 
37
- self.send_response(200)
39
+ req_ip = ip_address(self.client_address[0])
38
40
 
39
- self.end_headers()
41
+ # IPアドレスによる簡易的なアクセス制限
40
42
 
41
- self.wfile.write(self.to_content().encode('utf-8'))
43
+ # ループバック,ローカルアドレス以外はステータスコード:403を返す
42
44
 
45
+ is_accepted = any([req_ip.is_loopback, req_ip.is_private])
46
+
47
+ if is_accepted:
48
+
49
+ self.send_response(200)
50
+
51
+ self.end_headers()
52
+
53
+ self.wfile.write(self.to_content().encode('utf-8'))
54
+
43
- self.wfile.write(b'\n')
55
+ self.wfile.write(b'\n')
56
+
57
+ return
58
+
59
+ else:
60
+
61
+ self.send_response(403)
62
+
63
+ self.end_headers()
64
+
65
+ return
44
66
 
45
67
 
46
68
 
@@ -62,7 +84,7 @@
62
84
 
63
85
  print(os.path.abspath(__file__))
64
86
 
65
- with ThreadingHTTPServer(("", PORT), MyHandler) as httpd:
87
+ with ThreadingHTTPServer(("", PORT), MyHandler) as httpd:
66
88
 
67
89
  print("serving at port", PORT)
68
90
 
@@ -79,11 +101,3 @@
79
101
 
80
102
 
81
103
  ```
82
-
83
-
84
-
85
- ◇お願い。
86
-
87
- 外部からローカルパスを公開するのはセキュリティ的にアレではと思いつつ。
88
-
89
- アクセス制限とかを絶対に掛けてくださいな。

2

html#escapeをしていなかった件について

2018/04/14 11:45

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -3,6 +3,8 @@
3
3
 
4
4
 
5
5
  ```Python
6
+
7
+ # -*- coding: utf8 -*-
6
8
 
7
9
  # -*- coding: utf8 -*-
8
10
 
@@ -12,7 +14,7 @@
12
14
 
13
15
  from socketserver import ThreadingMixIn
14
16
 
15
- from string import Template
17
+ import html
16
18
 
17
19
 
18
20
 
@@ -20,23 +22,13 @@
20
22
 
21
23
  class MyHandler(SimpleHTTPRequestHandler):
22
24
 
25
+ def to_content(self) ->str:
23
26
 
27
+ body = f"time:{html.escape(self.date_time_string())}<br>dir:{html.escape(os.getcwd())}"
24
28
 
25
- def to_html(self):
29
+ content = f"<html><head></head><body>{body}</body></html>"
26
30
 
27
- t = Template('<br>time:$time<br>cwd:$cwd')\
28
-
29
- .substitute(time=self.date_time_string(), cwd=os.getcwd())
30
-
31
- body = """<html><head></head><body>
32
-
33
- {0}
34
-
35
- </body></html>
36
-
37
- """.format(t)
38
-
39
- return body
31
+ return content
40
32
 
41
33
 
42
34
 
@@ -46,7 +38,7 @@
46
38
 
47
39
  self.end_headers()
48
40
 
49
- self.wfile.write(self.to_html().encode('utf-8'))
41
+ self.wfile.write(self.to_content().encode('utf-8'))
50
42
 
51
43
  self.wfile.write(b'\n')
52
44
 

1

追記

2018/04/14 06:47

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -42,7 +42,7 @@
42
42
 
43
43
  def do_GET(self):
44
44
 
45
- self.send_response(300)
45
+ self.send_response(200)
46
46
 
47
47
  self.end_headers()
48
48