我是Python和Docker初学者,因此,我不确定错误所在。
Python 3.6.4 Docker版本18.03.0-ce,build 0520e24
这是python包在磁盘上的外观:
结构:
python-packaging-dependencies
jsonops
pyjo_demo.py
pyjo_spec.py
__init__.py
__pycache__
LICENSE.txt
MANIFEST.in
README.rst
setup.cfg
setup.py
__init__.py
主要文件如下。 pyjo_spec.py
from pyjo import Model, Field, RangeField, EnumField
from enum import Enum
class Gender(Enum):
female = 0
male = 1
class Address(Model):
city = Field(type=str)
postal_code = Field(type=int)
address = Field()
class User(Model):
name = Field(type=str, repr=True, required=True)
age = RangeField(min=18, max=120)
# equivalent to: Field(type=int, validator=lambda x: 18 <= x <= 120)
gender = EnumField(enum=Gender)
address = Field(type=Address)
pyjo_demo.py
from jsonops.pyjo_spec import Gender, Address, User
def to_dictionary():
u = User(name='john', age=18, address=Address(city='NYC'))
print(u.to_dict())
# {
# "name": "john",
# "age": 18,
# "address": {
# "city": "NYC"
# }
# }
def from_dictionary():
u = User.from_dict({
"name": "john",
"gender": "male",
"age": 18,
"address": {
"city": "NYC"
}
})
print(u)
# <User(name=john)>
print(u.gender)
# Gender.male
print(u.address.city)
# NYC
在我的本地Windows /远程Linux机器上,我调用了演示文件
Dockerfile
# Use an official Python runtime as a parent image
FROM python:3.6.4
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# Install any needed packages specified in requirements.txt
RUN pip install python_packaging_dependencies-1.0-py3-none-any.whl -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
app.py文件。这里 - 调用/ toDictionary工作正常(只返回一个静态字符串),前提是返回u.to_dict()被注释
from flask import Flask
import os
import socket
import jsonops
import jsonops.pyjo_spec
from jsonops.pyjo_spec import Gender, Address, User
app = Flask(__name__)
@app.route("/fromDictionary")
def from_dictionary():
u = User.from_dict({
"name": "john",
"gender": "male",
"age": 18,
"address": {
"city": "NYC"
}
})
print(u)
#<User(name=john)>
print(u.gender)
#Gender.male
print(u.address.city)
#NYC
#return u
return u.gender
@app.route("/toDictionary")
def to_dictionary():
u = User(name='john', age=18, address=Address(city='NYC'))
print(u.to_dict())
#return u.to_dict()
return "toDictionary"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80)
来自/来自词典的调用失败
ERROR in app: Exception on /fromDictionary [GET]
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1615, in full_dispatch_request
return self.finalize_request(rv)
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1630, in finalize_request
response = self.make_response(rv)
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1740, in make_response
rv = self.response_class.force_type(rv, request.environ)
File "/usr/local/lib/python3.6/site-packages/werkzeug/wrappers.py", line 921, in force_type
response = BaseResponse(*_run_wsgi_app(response, environ))
File "/usr/local/lib/python3.6/site-packages/werkzeug/wrappers.py", line 59, in _run_wsgi_app
return _run_wsgi_app(*args)
File "/usr/local/lib/python3.6/site-packages/werkzeug/test.py", line 923, in run_wsgi_app
app_rv = app(environ, start_response)
TypeError: 'Gender' object is not callable
10.254.70.61 - - [26/Mar/2018 17:20:27] "GET /fromDictionary HTTP/1.1" 500 -
当我停止容器时,仍会打印这些东西
[26/Mar/2018 17:20:27] "GET /fromDictionary HTTP/1.1" 500 -
^C<User(name=john)>
Gender.male
NYC
我错过了什么?
答案 0 :(得分:1)
我认为问题出在return u.gender
方法中尝试@app.route("/fromDictionary")
的地方。它不是一个字符串,所以烧瓶不知道如何处理它通过HTTP发送回来。您可以尝试将其编码为JSON数据或其他可以作为明文响应发送的格式。