我使用Flask构建了一个基本的Web应用程序,并且能够使用其本机http服务器从虚拟机运行它.我很快意识到,通过这个设置,请求被阻止(我无法对资源进行并发请求;任何新请求都要等到早期请求完成),并决定尝试使用gunicorn运行应用程序来解决此问题.我按照文档,特别是使用此行运行:
gunicorn -w 4 -b 127.0.0.1:4000 myproject:app
Run Code Online (Sandbox Code Playgroud)
但是,它无法启动这样做,并抱怨没有WSGI应用程序.在互联网上闲聊,我发现有很多人发布了以下示例:
from werkzeug.contrib.fixers import ProxyFix
app.wsgi_app = ProxyFix(app.wsgi_app)
Run Code Online (Sandbox Code Playgroud)
我补充说,它解决了我的问题.我很困惑,因为这显然是为了解决在HTTP代理后面服务的问题,但是增加的gunicorn会强加一个HTTP代理吗?或者我总是落后于代理,而Flask的内置服务器并不重要?
此外,Werkzeug关于Fixers的文档警告"出于安全原因,不要在非代理设置中使用此中间件." 考虑到修复显然是必要的,我可以假设我正在进行代理设置吗?
我xml在我的postgresql数据库中使用,我需要一个自定义类型可以处理xmlSQLAlchemy中的数据.
所以我让XMLType课堂沟通xml.etree,但它没有按照我的意愿工作.
这是我写的代码:
import xml.etree.ElementTree as etree
class XMLType(sqlalchemy.types.TypeDecorator):
impl = sqlalchemy.types.UnicodeText
type = etree.Element
def get_col_spec(self):
return 'xml'
def bind_processor(self, dialect):
def process(value):
if value is not None:
return etree.dump(value)
else:
return None
return process
def process_result_value(self, value, dialect):
if value is not None:
value = etree.fromstring(value)
return value
Run Code Online (Sandbox Code Playgroud)
它适用于检索值和结果处理.但是当我试图插入一行时,我得到一个错误(当然,我把bodyas xml.etree.ElementTree.Element对象):
IntegrityError: (IntegrityError) null value in column "body" violates not-null
constraint "INSERT INTO comments (id, author_id, look_id, body, …Run Code Online (Sandbox Code Playgroud) 有两个表,表A的一列指向另一个表B的主键.
但是它们被放在不同的数据库中,所以我无法用外键配置它们.
配置via relationship()不可用,所以我手动实现了属性属性.
class User(Base):
__tablename__ = 'users'
id = Column(BigInteger, id_seq, primary=True)
name = Column(Unicode(256))
class Article(Base):
__tablename__ = 'articles'
__bind_key__ = 'another_engine'
# I am using custom session configures bind
# each mappers to multiple database engines via this attribute.
id = Column(BigInteger, id_seq, primary=True)
author_id = Column(BigInteger, nullable=False, index=True)
body = Column(UnicodeText, nullable=False)
@property
def author(self):
_session = object_session(self)
return _session.query(User).get(self.author_id)
@author.setter
def author(self, user):
if not isinstance(user, User):
raise TypeError('user must be a instance …Run Code Online (Sandbox Code Playgroud) 假设我有一个两元素元组列表和一个(非元组)文字列表,例如整数:
a = [('x', 'a'), ('y', 'b'), ('z', 'c')]
b = [1, 2 ,3]
Run Code Online (Sandbox Code Playgroud)
我想列出三元素元组,所以我编码如下:
zipped = zip((t[0] for t in a), (t[1] for t in a), b)
assert zipped == [('x', 'a', 1), ('y', 'b', 2), ('z', 'c', 3)]
Run Code Online (Sandbox Code Playgroud)
我当前的代码工作得很好,但我想知道有更高效和优雅的配方,但我的代码必须迭代并解压缩每个元组两次.任何人都可以建议吗?
import requests
endpoint = 'http://data.alexa.com/data?'
qparams = {'cli': 10,
'url': 'www.google.com'}
response = requests.get(endpoint, params=qparams)
print response.url
Run Code Online (Sandbox Code Playgroud)
这告诉我它查看了http://data.alexa.com/data?url=www.google.com&cli=10
哪个网址错误,应该是http://data.alexa.com/data?cli=10&url=www.google.com
有人可以帮忙吗?
python ×4
sqlalchemy ×2
elementtree ×1
flask ×1
gunicorn ×1
list ×1
orm ×1
postgresql ×1
python-2.7 ×1
werkzeug ×1
wsgi ×1
xml ×1