Apache/2.4.7(Ubuntu)でflask_script.pyを動作させているのですが,http://163.225.*.*にLAN内の別PCからmypost.pyでPOSTした時にerrorが出てしまいます.下記にエラー.
一応,webサーバにはアクセスできているようで,ubuntuのipアドレスを打てばconvert to imageと正しく表示されます.
またcurlコマンドでpost_testにjsonを送って見てもコマンドラインにjsonのvalueが表示され,正しく動作していると考えられます.
なぜmypost.pyでうまく遅れていないのでしょうか.
apache2で動作させず,同じPCのローカル2端末だとうまく動きます.
python
1#flask_script.py 2from flask import Flask 3from flask import request 4import base64 5 6app = Flask(__name__) 7 8def convert_b64_to_file(b64,outfile_path): 9 """ 10 b64をデコードしてファイルに書き込む 11 """ 12 s = base64.decodestring(b64) 13 with open(outfile_path,"wb") as f : 14 f.write(s) 15 16@app.route("/") 17def index(): 18 return "convert to image" 19 20#base64でエンコードされたjsonファイルをデコード 21@app.route('/post_request', methods=['POST']) 22def post_request(): 23 #request.jsonでにPOSTされたデータを取ってくる 24 temp = request.json["key"] 25 #byteに変換 26 temp = bytes(temp,"utf-8") 27 #デコード 28 convert_b64_to_file(temp,"./copy1.png") 29 return temp 30 31@app.route('/post_test', methods=['POST']) 32def test(): 33 #request.jsonでにPOSTされたデータを取ってくる 34 temp = request.json["key"] 35 #byteに変換 36 temp = bytes(temp,"utf-8") 37 return temp 38 39 40if __name__ == "__main__": 41 app.debug = True 42 app.run(host="0.0.0.0")
python
1#mypost.py 2import urllib.request, json 3import base64 4 5def convert_b64(file_path): 6 """ 7 b64にエンコード 8 """ 9 return base64.encodestring(open(file_path, 'rb').read()).decode("utf-8") 10 11if __name__ == '__main__': 12 url = "http://163.225.*.*/post_request" 13 image = "test.jpg" 14 method = "POST" 15 headers = {"Content-Type" : "application/json"} 16 value = convert_b64(image) 17 # PythonオブジェクトをJSONに変換する 18 obj = {"key" : value} 19 json_data = json.dumps(obj).encode("utf-8") 20 # httpリクエストを準備してPOST 21 request = urllib.request.Request(url, data=json_data, method=method, headers=headers) 22 with urllib.request.urlopen(request) as response: 23 response_body = response.read().decode("utf-8") 24 25
Traceback (most recent call last): File "/Users/ir/anaconda/lib/python3.6/urllib/request.py", line 1318, in do_open encode_chunked=req.has_header('Transfer-encoding')) File "/Users/ir/anaconda/lib/python3.6/http/client.py", line 1239, in request self._send_request(method, url, body, headers, encode_chunked) File "/Users/ir/anaconda/lib/python3.6/http/client.py", line 1285, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "/Users/ir/anaconda/lib/python3.6/http/client.py", line 1234, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "/Users/ir/anaconda/lib/python3.6/http/client.py", line 1026, in _send_output self.send(msg) File "/Users/ir/anaconda/lib/python3.6/http/client.py", line 964, in send self.connect() File "/Users/ir/anaconda/lib/python3.6/http/client.py", line 936, in connect (self.host,self.port), self.timeout, self.source_address) File "/Users/ir/anaconda/lib/python3.6/socket.py", line 722, in create_connection raise err File "/Users/ir/anaconda/lib/python3.6/socket.py", line 713, in create_connection sock.connect(sa) ConnectionRefusedError: [Errno 61] Connection refused During handling of the above exception, another exception occurred: Traceback (most recent call last): File "mypost.py", line 21, in <module> with urllib.request.urlopen(request) as response: File "/Users/ir/anaconda/lib/python3.6/urllib/request.py", line 223, in urlopen return opener.open(url, data, timeout) File "/Users/ir/anaconda/lib/python3.6/urllib/request.py", line 526, in open response = self._open(req, data) File "/Users/ir/anaconda/lib/python3.6/urllib/request.py", line 544, in _open '_open', req) File "/Users/ir/anaconda/lib/python3.6/urllib/request.py", line 504, in _call_chain result = func(*args) File "/Users/ir/anaconda/lib/python3.6/urllib/request.py", line 1346, in http_open return self.do_open(http.client.HTTPConnection, req) File "/Users/ir/anaconda/lib/python3.6/urllib/request.py", line 1320, in do_open raise URLError(err) urllib.error.URLError: <urlopen error [Errno 61] Connection refused>```
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。