質問するログイン新規登録

質問編集履歴

4

修正

2021/01/21 06:19

投稿

I_am_
I_am_

スコア23

title CHANGED
File without changes
body CHANGED
@@ -183,12 +183,12 @@
183
183
  ```
184
184
 
185
185
  ```
186
- root@f144c53506cb:/media/docker_shared/mono_develop/processing/src/python_cgi_src/cgi_bin# python run_detection_test.py 5000
186
+ root@80094834d94e:/media/docker_shared/mono_develop/processing/src/python_cgi_src/cgi_bin# python run_detection.py 5000
187
- 172.19.0.1 - - [21/Jan/2021 13:49:59] "GET /ok-api HTTP/1.1" 200 -
187
+ 153.240.33.132 - - [21/Jan/2021 15:17:27] "GET /ok-api HTTP/1.1" 200 -
188
188
  Face: [175 x 175 from (216, 202)]
189
189
  finish (no error)
190
190
  ----------------------------------------
191
- Exception happened during processing of request from ('172.19.0.1', 63810)
191
+ Exception happened during processing of request from ('153.240.33.132', 1563)
192
192
  Traceback (most recent call last):
193
193
  File "/usr/lib/python3.6/socketserver.py", line 320, in _handle_request_noblock
194
194
  self.process_request(request, client_address)
@@ -196,9 +196,9 @@
196
196
  self.finish_request(request, client_address)
197
197
  File "/usr/lib/python3.6/socketserver.py", line 364, in finish_request
198
198
  self.RequestHandlerClass(request, client_address, self)
199
- File "/media/docker_shared/mono_develop/processing/src/python_cgi_src/cgi_bin/python_http_test.py", line 13, in handler
199
+ File "/media/docker_shared/mono_develop/processing/src/python_cgi_src/cgi_bin/python_http.py", line 12, in handler
200
200
  MyHTTPRequestHandler(callback, *args)
201
- File "/media/docker_shared/mono_develop/processing/src/python_cgi_src/cgi_bin/python_http_test.py", line 23, in __init__
201
+ File "/media/docker_shared/mono_develop/processing/src/python_cgi_src/cgi_bin/python_http.py", line 21, in __init__
202
202
  BaseHTTPRequestHandler.__init__(self, *args)
203
203
  File "/usr/lib/python3.6/socketserver.py", line 724, in __init__
204
204
  self.handle()
@@ -206,11 +206,14 @@
206
206
  self.handle_one_request()
207
207
  File "/usr/lib/python3.6/http/server.py", line 406, in handle_one_request
208
208
  method()
209
- File "/media/docker_shared/mono_develop/processing/src/python_cgi_src/cgi_bin/python_http_test.py", line 34, in do_GET
209
+ File "/media/docker_shared/mono_develop/processing/src/python_cgi_src/cgi_bin/python_http.py", line 31, in do_GET
210
210
  self.wfile.write("OK")
211
211
  File "/usr/lib/python3.6/socketserver.py", line 803, in write
212
212
  self._sock.sendall(b)
213
213
  TypeError: a bytes-like object is required, not 'str'
214
+ ----------------------------------------
215
+ 153.240.33.132 - - [21/Jan/2021 15:17:28] code 400, message NG!
216
+ 153.240.33.132 - - [21/Jan/2021 15:17:28] "GET /favicon.ico HTTP/1.1" 400 -
214
217
  ```
215
218
 
216
219
  TypeError: a bytes-like object is required, not 'str'とエラーがでます。

3

追記

2021/01/21 06:19

投稿

I_am_
I_am_

スコア23

title CHANGED
File without changes
body CHANGED
@@ -212,4 +212,9 @@
212
212
  self._sock.sendall(b)
213
213
  TypeError: a bytes-like object is required, not 'str'
214
214
  ```
215
+
215
- TypeError: a bytes-like object is required, not 'str'とエラーがでます。
216
+ TypeError: a bytes-like object is required, not 'str'とエラーがでます。
217
+ どこでエラーが出ているのかがわかりません。
218
+
219
+ また、
220
+ AWSのDockerコンテナ間でのHTTP通信の場合は、http://awsのip:5000/ok-apiにgetリクエストをかければ動作するという認識であっていますか?

2

追記

2021/01/21 06:09

投稿

I_am_
I_am_

スコア23

title CHANGED
File without changes
body CHANGED
@@ -67,4 +67,149 @@
67
67
  ```
68
68
  スクリプトが実行された場合、サーバが受付モードになり、http://localhost:5000/ok-apiにアクセスがあった場合に該当の do_GETが動くと思ったのですが、実際にはスクリプトを実行すると、do_GETが実行されてしまいます。
69
69
  また、http://localhost:5000/ok-apiにアクセスすると、サーバが動いていないみたいなのですが、なぜなのでしょうか?
70
- 何を確認すればいいか分からないので教えてください。
70
+ 何を確認すればいいか分からないので教えてください。
71
+
72
+ ### 追記
73
+
74
+ 違うサイトから、書き方を変更してみました。
75
+ python_http_server.py
76
+ ```
77
+ # coding: utf-8
78
+ from http.server import BaseHTTPRequestHandler, HTTPServer,\
79
+ SimpleHTTPRequestHandler
80
+ from urllib.parse import parse_qs, urlparse
81
+
82
+ def start(port, callback):
83
+ def handler(*args):
84
+ MyHTTPRequestHandler(callback, *args)
85
+ server = HTTPServer(('', int(port)), handler)
86
+ server.serve_forever()
87
+
88
+
89
+ # ハンドラを定義していく
90
+ class MyHTTPRequestHandler(BaseHTTPRequestHandler):
91
+ def __init__(self, callback, *args):
92
+ self.callback = callback
93
+ BaseHTTPRequestHandler.__init__(self, *args)
94
+
95
+ def do_GET(self):
96
+ parsed_path = urlparse.urlparse(self.path)
97
+
98
+ # 成功レスポンス(200)を返す
99
+ if parsed_path.path == "/ok-api":
100
+ self.send_response(200)
101
+ self.end_headers()
102
+ self.callback()
103
+ self.wfile.write("OK")
104
+ return
105
+
106
+ # 失敗レスポンス(403)を返す
107
+ elif parsed_path.path == "/ng-api":
108
+ self.send_error(403, "NG!")
109
+ self.end_headers()
110
+ return
111
+
112
+ # クエリパラメータ("left-str", "right-str")を連結した文字列を返す
113
+ # /concat-str?left-str=Hello&right-str=World
114
+ elif parsed_path.path == "/concat-str":
115
+ # クエリパラメータのパース(dictionary型)
116
+ querys = urlparse.parse_qs(parsed_path.query)
117
+ if ("left-str" in querys) and ("right-str" in querys):
118
+ concat_str = querys["left-str"][0] + querys["right-str"][0]
119
+ self.send_response(200)
120
+ self.end_headers()
121
+ self.wfile.write(concat_str)
122
+
123
+ else:
124
+ #"left-str"と"right-str"のクエリがなかったらエラー
125
+ self.send_error(400, "query NG!")
126
+ self.end_headers()
127
+ return
128
+
129
+ # Jsonを返す
130
+ elif parsed_path.path == "/return-json":
131
+ data = [{u"name":u"尾崎豊", u"age":26},
132
+ {u"name":u"hide", u"age":33}]
133
+ jsonData = json.dumps(data, ensure_ascii=False, encoding='utf-8')
134
+
135
+ self.send_response(200)
136
+ self.send_header("Content-type", "application/json")
137
+ self.end_headers()
138
+
139
+ self.wfile.write(jsonData.encode("utf-8"))
140
+ return
141
+
142
+ else:
143
+ self.send_error(400, "NG!")
144
+ self.end_headers()
145
+ return
146
+ ```
147
+
148
+ run_detection.py
149
+ ```
150
+ import sys
151
+ sys.path.append('site-packagesのパス')
152
+ from skimage import data, io, filters, color, img_as_ubyte
153
+ from sklearn import preprocessing
154
+ import numpy as np
155
+ import matplotlib.pyplot as plt
156
+ import scipy
157
+ import Cython
158
+
159
+ # C++で作成した共有ファイルの.soをインポート
160
+ import face_detection
161
+ # httpserver
162
+ import sys
163
+ import python_http
164
+
165
+ input_path = '/media/docker_shared/mono_develop/img/getImg.jpg'
166
+ output_path = '/media/docker_shared/mono_develop/img/kansei.jpg'
167
+
168
+ def call_processing():
169
+ try :
170
+
171
+ result_c_img = face_detection.face_detect()
172
+
173
+ except:
174
+ print('Error')
175
+
176
+ else:
177
+ print("finish (no error)")
178
+
179
+ if __name__ == '__main__':
180
+ port = sys.argv[1]
181
+ pythoon_http.start(port, call_processing)
182
+
183
+ ```
184
+
185
+ ```
186
+ root@f144c53506cb:/media/docker_shared/mono_develop/processing/src/python_cgi_src/cgi_bin# python run_detection_test.py 5000
187
+ 172.19.0.1 - - [21/Jan/2021 13:49:59] "GET /ok-api HTTP/1.1" 200 -
188
+ Face: [175 x 175 from (216, 202)]
189
+ finish (no error)
190
+ ----------------------------------------
191
+ Exception happened during processing of request from ('172.19.0.1', 63810)
192
+ Traceback (most recent call last):
193
+ File "/usr/lib/python3.6/socketserver.py", line 320, in _handle_request_noblock
194
+ self.process_request(request, client_address)
195
+ File "/usr/lib/python3.6/socketserver.py", line 351, in process_request
196
+ self.finish_request(request, client_address)
197
+ File "/usr/lib/python3.6/socketserver.py", line 364, in finish_request
198
+ self.RequestHandlerClass(request, client_address, self)
199
+ File "/media/docker_shared/mono_develop/processing/src/python_cgi_src/cgi_bin/python_http_test.py", line 13, in handler
200
+ MyHTTPRequestHandler(callback, *args)
201
+ File "/media/docker_shared/mono_develop/processing/src/python_cgi_src/cgi_bin/python_http_test.py", line 23, in __init__
202
+ BaseHTTPRequestHandler.__init__(self, *args)
203
+ File "/usr/lib/python3.6/socketserver.py", line 724, in __init__
204
+ self.handle()
205
+ File "/usr/lib/python3.6/http/server.py", line 418, in handle
206
+ self.handle_one_request()
207
+ File "/usr/lib/python3.6/http/server.py", line 406, in handle_one_request
208
+ method()
209
+ File "/media/docker_shared/mono_develop/processing/src/python_cgi_src/cgi_bin/python_http_test.py", line 34, in do_GET
210
+ self.wfile.write("OK")
211
+ File "/usr/lib/python3.6/socketserver.py", line 803, in write
212
+ self._sock.sendall(b)
213
+ TypeError: a bytes-like object is required, not 'str'
214
+ ```
215
+ TypeError: a bytes-like object is required, not 'str'とエラーがでます。

1

追記

2021/01/21 06:03

投稿

I_am_
I_am_

スコア23

title CHANGED
File without changes
body CHANGED
@@ -13,16 +13,9 @@
13
13
  Face: [175 x 175 from (216, 202)]
14
14
  finish (no error)
15
15
  ```
16
- http://localhost:5000/ok-apiにアクセスしてみると以下です。
16
+ http://localhost:5000/ok-apiにアクセスしてみるとサーバが動作していないみたいです。
17
- ```
18
- Error response
19
- Error code: 404
20
17
 
21
- Message: File not found.
22
18
 
23
- Error code explanation: HTTPStatus.NOT_FOUND - Nothing matches the given URI.
24
- ```
25
-
26
19
  ### 該当のソースコード
27
20
 
28
21
  ```python
@@ -72,4 +65,6 @@
72
65
  server = HTTPServer(address, MyHTTPRequestHandler)#サーバインスタンスを生成
73
66
  server.serve_forever()#常時受けつけモードを指定。
74
67
  ```
75
- スクリプトが実行された場合、サーバが受付モードになり、http://localhost:5000/ok-apiにアクセスがあった場合に該当の do_GETが動くと思ったのですが、実際にはスクリプトを実行すると、do_GETが実行されてしまいます。
68
+ スクリプトが実行された場合、サーバが受付モードになり、http://localhost:5000/ok-apiにアクセスがあった場合に該当の do_GETが動くと思ったのですが、実際にはスクリプトを実行すると、do_GETが実行されてしまいます。
69
+ また、http://localhost:5000/ok-apiにアクセスすると、サーバが動いていないみたいなのですが、なぜなのでしょうか?
70
+ 何を確認すればいいか分からないので教えてください。