我正在Mac上使用GoogleAppEngineLauncher浏览GAE webapp2教程(运行时2.7),虽然我正在完全遵循所有内容,但我在导入Jinja2模块时仍然出现错误:使用模板 - Google App Engine
错误:
回溯(最近一次调用最后一次):文件"/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py",第168行,在Handle处理程序中= _config_handle.add_wsgi_middleware(self._LoadHandler())文件"/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py",第206行, in _LoadHandler handler = import(path [0])File"/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py",第640行,在Decorate return func(self,*args,**kwargs)文件"/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py",第1756行,在load_module中返回self.FindAndLoadModule(submodule,fullname,search_path)文件"/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py",第640行,在Decorate return func( self,*args,**kwargs)在FindAndLoadModule中输入文件"/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py",第1628行描述)文件"/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py",第640行,装饰返回功能(self,*args ,**kwargs)文件"/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py",第1571行,在LoadModuleRestricted说明中)文件"/用户/ ykessler/Dropbox/appgrinders/gae_apps/helloworld2.7/helloworld.py",第9行,导入jinja2 ImportError:没有名为jinja2的模块
所以它出于某种原因无法找到该模块,即使它应该被打包为webapp2的附加功能的一部分.当我在我的文件系统上搜索时,它看起来像是在那里:
/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2_extras/jinja2.py
Run Code Online (Sandbox Code Playgroud)
如果我将应用程序部署到GAE,它可以正常工作.为什么会在本地失败?
谷歌appengine的webapp2有关于上传文件处理的非常神秘的文档.
Uploaded files are available as cgi.FieldStorage (see the cgi module) instances directly in request.POST.
Run Code Online (Sandbox Code Playgroud)
我有一个表单,它发出我想要存储在NDB.JsonProperty中的JSON文件的POST请求.
任何人都可以提供一个简短的例子,说明如何从请求对象中读取文件?
如何从Google App Engine WebApp2中的Angular POST请求中获取数据? self.request.body返回一个字符串,不self.request.get(key)返回任何内容.
提交POST的Angular代码是:
$http.post("/mail", {request_name: 'Test Name', request_body: 'Test Body'});
Run Code Online (Sandbox Code Playgroud)
然后我的WebApp2处理程序中的这两行:
print "1: " + self.request.body
print "2: " + self.request.get('request_name')
Run Code Online (Sandbox Code Playgroud)
打印此:
1: {"request_name":"Test Name","request_body":"Test Body"}
2:
Run Code Online (Sandbox Code Playgroud)
从POST正文中获取数据的最佳方法是什么?或者我应该以不同方式发送请求?
尝试从数据存储区读取非ascii时,我收到一条奇怪的错误消息:
'ascii' codec can't decode byte 0xc3 in position 5: ordinal not in range(128)
Traceback (most recent call last):
File "/base/data/home/apps/s~myapp-www/events.355951895377615944/webapp2.py", line 1511, in __call__
rv = self.handle_exception(request, response, e)
File "/base/data/home/apps/s~myapp-www/events.355951895377615944/webapp2.py", line 1505, in __call__
rv = self.router.dispatch(request, response)
File "/base/data/home/apps/s~myapp-www/events.355951895377615944/webapp2.py", line 1253, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/data/home/apps/s~myapp-www/events.355951895377615944/webapp2.py", line 1077, in __call__
return handler.dispatch()
File "/base/data/home/apps/s~myapp-www/events.355951895377615944/handler.py", line 127, in dispatch
response = super(NewBaseHandler, self).dispatch()
File "/base/data/home/apps/s~myapp-www/events.355951895377615944/webapp2.py", line 547, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/data/home/apps/s~myapp-www/events.355951895377615944/webapp2.py", line 545, …Run Code Online (Sandbox Code Playgroud) 我正在尝试在Python脚本中编码对象并将其设置为cookie,以便我可以使用客户端JavaScript来读取它.
我试图做的每一种方式都遇到了问题.通常,cookie的格式化方式使JSON.parse()中断.
我目前的剧本:
cookie = Cookie.SimpleCookie()
data = {"name": "Janet", "if_nasty": "Ms. Jackson"}
cookie['test'] = json.dumps(data)
self.response.headers.add_header("Set-Cookie", cookie.output(header=''))
Run Code Online (Sandbox Code Playgroud)
...导致
test="{\"name\": \"janet\"\054 \"if_nasty\": \"Ms. Jackson\"}"
Run Code Online (Sandbox Code Playgroud)
在客户端上.
我真的不想在它们出现时引入一个hack-y解决方案来替换逗号实例.我有什么想法可以通过Python 传递复杂的数据结构(通过设置和读取cookie)?
原始问题
App Engine SDK 1.6.5
Python 2.7
webapp2
我已经实现了webapp2方案来保护页面到https.问题是,当用户说http:// site/login而不是https:// site/login时,由于方案无法识别路由,因此会出现404错误.
示例main.py
# Libraries
import webapp2
# Local Controllers
from controllers.HomeHandler import HomeHandler
from controllers.LoginHandler import LoginHandler
app = webapp2.WSGIApplication([
webapp2.Route(r'/', HomeHandler),
webapp2.Route(r'/login', LoginHandler, schemes=['https'], name='login')
], debug=True)
Run Code Online (Sandbox Code Playgroud)
我在https路由下面添加了另一个路由/控制器来捕获http请求:
webapp2.Route(r'/login', RouteLogin)
RouteLogin.py
# Libraries
import webapp2
class RouteLogin(webapp2.RequestHandler):
def get(self):
self.redirect('https://site.appspot.com/login')
Run Code Online (Sandbox Code Playgroud)
这有效,但似乎应该有更好的方法来做到这一点.就像在Apache Web服务器上使用htaccess一样.这对我喜欢的黑客来说太像了.我真的不喜欢我的代码中的硬编码URL.更不用说登录的2个请求并不是什么大不了的事,但可能还有其他一些例子,它最终导致成本太高.
注意1:如果您正在查看此解决方案,请注意使用HTTPS方案还意味着您将无法在不删除SCHEME或配置为dev设置的变量的情况下使用开发控制台.
注意2:我能够以编程方式提供HTTPS而不是HTTP.我在正确的轨道上有下面的评论,但它需要一个参数.
webapp2.uri_for('login', _scheme='https')
这将为您提供正确的https://someapp.appspot.com/login网址.遗憾的是,我没有处理我的主要问题,如何处理人们在没有https的情况下在地址栏中键入url并收到错误,除非我使用上面的hack.所以我仍然在寻找WSGI方式将收入请求路由到HTPPS.
编辑:添加了注释1并澄清了标题,我认为很明显我是从源代码而不是CGI使用WSGI.
我对单元测试很陌生,并试图找出最佳实践.我在这里看到了几个关于unit-test继承一个基类的问题,这个基类本身包含几个测试,例如:
class TestBase(unittest.TestCase):
# some standard tests
class AnotherTest(TestBase):
# run some more tests in addition to the standard tests
Run Code Online (Sandbox Code Playgroud)
我认为我从社区收集的是,为每个实现编写单独的测试并使用多重继承更好.但是,如果该基类实际上不包含任何测试 - 只是所有其他测试的助手.例如,假设我有一些基础测试类,我曾经用它来存储一些常用方法,如果不是所有其他测试都会使用这些方法.我们还假设我有一个models.py被调用的数据库模型ContentModel
test_base.py
import webtest
from google.appengine.ext import testbed
from models import ContentModel
class TestBase(unittest.TestCase):
def setUp(self):
self.ContentModel = ContentModel
self.testbed = testbed.Testbed()
self.testbed.activate()
# other useful stuff
def tearDown(self):
self.testbed.deactivate()
def createUser(self, admin=False):
# create a user that may or may not be an admin
# possibly other useful things
Run Code Online (Sandbox Code Playgroud)
这似乎可以节省我所有其他测试的大量时间:
another_test.py
from …Run Code Online (Sandbox Code Playgroud) 在线查看webapp2文档时,我发现了装饰器的信息webapp2.cached_property(可以在https://webapp-improved.appspot.com/api/webapp2.html#webapp2.cached_property找到).
在文档中,它说:
将函数转换为惰性属性的装饰器.
我的问题是:
谢谢!
我认为库中存在内存泄漏ndb但我无法找到.
有没有办法避免下面描述的问题?
您是否有更准确的测试想法来确定问题所在?
这就是我重现问题的方式:
我创建了一个包含2个文件的简约Google App Engine应用程序.
app.yaml:
application: myapplicationid
version: demo
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /.*
script: main.APP
libraries:
- name: webapp2
version: latest
Run Code Online (Sandbox Code Playgroud)
main.py:
# -*- coding: utf-8 -*-
"""Memory leak demo."""
from google.appengine.ext import ndb
import webapp2
class DummyModel(ndb.Model):
content = ndb.TextProperty()
class CreatePage(webapp2.RequestHandler):
def get(self):
value = str(102**100000)
entities = (DummyModel(content=value) for _ in xrange(100))
ndb.put_multi(entities)
class MainPage(webapp2.RequestHandler):
def get(self):
"""Use of `query().iter()` was suggested here:
https://code.google.com/p/googleappengine/issues/detail?id=9610 …Run Code Online (Sandbox Code Playgroud) python google-app-engine memory-leaks webapp2 app-engine-ndb
在webapp2 文档中没有提到设置SameSitecookie的属性,它似乎是建立在 WebOB 的响应处理程序上的,我检查了 webOB 文档页面,它清楚地显示了'SameSite' flag as an accepted cookie parameter
尽管如此,我还是尝试在 set cookie 中设置它:
self.response.set_cookie(name, secure_cookie, path='/', secure=True,
httponly=True, samesite='lax', expires=expireDate)
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
TypeError: set_cookie() got an unexpected keyword argument 'samesite'
我知道可以使用,self.response.headers.add_header('Set-Cookie', ...但我希望我可以self.response.set_cookie按照 webapp2 文档使用
webapp2 ×10
python ×7
python-2.7 ×2
angularjs ×1
cookies ×1
https ×1
jinja2 ×1
json ×1
macos ×1
memory-leaks ×1
properties ×1
ssl ×1
testbed ×1
unicode ×1
unit-testing ×1
webob ×1
webtest ×1