在本地从另一个flask应用调用flask应用



我有两个Flask应用程序(A和B)。应用程序A在端口5000上本地运行,并向端口5002上运行的应用程序B发出post请求,如下所示:

headers = {
'accept': 'application/type',
'Content-Type': 'application/json'
}
data = '{"test_string": "test input string", "flatten": true}'
response = requests.post('http://localhost:5002/endpoint', headers=headers, data=data)

,返回的响应是:

Max retries exceeded with url: /endpoint/ Failed to establish a new connection: [Errno 111] Connection refused

我打开了我的终端并尝试了这个curl请求:

curl -X 'POST' 'http://localhost:5002/endpoint/' -H 'accept: application/json' -H 'Content-Type: application/json' -d '{"test_string": "test input string", "flatten": true}'

,它从应用B

返回正确的响应我想也许有一个问题,我如何格式化应用程序a上的请求,因为curl正在工作,我测试了我在应用程序a上使用的3行从我的终端使用python,它在我的python终端工作。

唯一剩下的变量是app A是本地运行的flask应用程序,因为该代码片段运行在python终端中。从应用程序A调用应用程序B与两个应用程序在不同的端口本地运行是否有一些问题?


更新:好吧,我已经缩小了问题与docker-compose和Redis的东西。下面是如何复制它:

应用程序B:app.py

from flask import Flask, abort
from flask_restx import Api, Resource, fields
app = Flask(__name__)
api = Api(app, version='1.0', title='something',
description='something',
)
ns = api.namespace('parse', description='something')
parse_raw = api.model('parse', {
'input_string': fields.String(required=True, description='string')
})

@ns.route('/')
class Parser(Resource):
@ns.doc('parse')
@ns.expect(parse_raw)
def post(self):
'''Parse'''
try:
s = "yes"
except Exception:
abort(400, 'unsupported')
return s, 200

if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5002)

应用程序:app.py

from flask import Flask
from flask_restx import Api, Resource
import requests
app = Flask(__name__)
api = Api(app, version='1.0', title='something',
description='something',
)
ns = api.namespace('parse', description='something')

@ns.route('/')
class Parser(Resource):
@ns.doc('parse')
def get(self):
'''Parse'''
headers = {
'accept': 'application/json',
'Content-Type': 'application/json',
}
data = '{"input_string": "SELECT * FROM table1"}'
response = requests.post('http://0.0.0.0:5002/parse/', headers=headers, data=data)
return response.text, 200

if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)

docker-compose.yaml

version: '3'
services:
app:
build:
context: .
dockerfile: Dockerfile
ports:
- "5000:5000"
redis:
image: "redis:alpine"

Dockerfile

FROM python:3.6-alpine
WORKDIR /app
ENV PYTHONHASHSEED=1
RUN apk add --no-cache --update make
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . .
CMD ["python3", "app.py"]

redis
Flask~=1.1.2
flask-restx
tenacity
requests

你可以正常运行应用程序B,我如何运行应用程序A:docker-compose up --build

您可以从终端curl应用程序B:curl -X 'POST' 'http://localhost:5002/parse/' -H 'accept: application/json' -H 'Content-Type: application/json' -d '{ "input_string": "string" }'

,你也可以从python终端运行app A的代码:

headers = {
'accept': 'application/json',
'Content-Type': 'application/json',
}
data = '{"input_string": "SELECT * FROM table1"}'
response = requests.post('http://0.0.0.0:5002/parse/', headers=headers, data=data)

应用程序A不能调用应用程序B

如果你只运行app.py运行应用A,它工作得很好,它只会在使用docker-compose + redis时中断,即使redis甚至没有在示例中使用

明白了。Docker容器在不同的网络上运行,所以'localhost'不起作用。dockerization应用B并将其部署到与应用A相同的docker网络中,然后将'localhost'更改为网络中B的容器名称。

最新更新