天道酬勤,学无止境

pyramid

All addresses to go to a single page (catch-all route to a single view) in Python Pyramid

问题 我正在尝试更改 Pyramid hello world 示例,以便对 Pyramid 服务器的任何请求都服务于同一页面。 即所有路线都指向同一个视图。 这是iv到目前为止得到的: from wsgiref.simple_server import make_server from pyramid.config import Configurator from pyramid.response import Response def hello_world(request): return Response('Hello %(name)s!' % request.matchdict) if __name__ == '__main__': config = Configurator() config.add_route('hello', '/*') config.add_view(hello_world, route_name='hello') app = config.make_wsgi_app() server = make_server('0.0.0.0', 8080, app) server.serve_forever() iv 所做的只是更改行(来自 hello world 示例): config.add_route('hello', '/hello/{name}')

2022-05-11 22:10:19    分类:技术分享    python   pyramid

Writing doctests for pyramid web app which depend on settings in ini file

问题 我想使用 webtest 模块为我的金字塔网络应用程序编写文档测试。 我试过这样: from my_webapp import main from webtest import TestApp app = TestApp(main({})) result = app.get('/') 当我的代码到达这一行时,这会引发KeyError (因为some.url未知): url = request.registry.settings['some.url'] some.url的值在我的应用程序的 Paster ini 文件中指定。 在运行我的测试代码时,有没有一种简单的方法可以使用我的development.ini ? 我还没有完全理解在金字塔启动期间如何/何时加载 ini 文件,因此在测试时很难弄清楚在哪里加载它。 回答1 main使用您的 ini 文件的内容调用。 从 ini 加载应用程序的一种简单方法是: from pyramid.paster import get_app app = get_app('testing.ini#main') test_app = TestApp(app) 这期望“testing.ini”位于当前工作目录中,因此您可能需要对其进行调整。 如果您希望它与树中的某个位置相关,您可以使用: import os.path import some

2022-05-05 21:33:08    分类:技术分享    python   pyramid   wsgi   webtest

python Requests login to website returns 403

问题 我正在尝试使用requests登录网站,但您可以猜到我遇到了问题 这是我正在使用的代码 import requests EMAIL = '***' PASSWORD = '***' URL = 'https://portal.bitcasa.com/login' client = requests.session(config={'verbose': sys.stderr}) login_data = {'username': EMAIL, 'password': PASSWORD,} r = client.post(URL, data=login_data, headers={"Referer": "foo"}) print r 如果我打印出r.text我得到 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html lang="en"> <head><script type="text/javascript">var NREUMQ=NREUMQ||[];NREUMQ.push(["mark","firstbyte",new Date().getTime()])</script> <meta http-equiv=

2022-05-04 15:31:56    分类:技术分享    python   django   pyramid   python-requests

What does '@reify' do and when should it be used?

问题 我在 UX 设计的 Pyramid 教程中看到了它。 我无法弄清楚这个装饰器的全部内容。 我看到它的用法的示例代码。 def __init__(self, request): self.request = request renderer = get_renderer("templates/global_layout.pt") self.global_template = renderer.implementation().macros['layout'] @reify def company_name(self): return COMPANY @reify def site_menu(self): new_menu = SITE_MENU[:] url = self.request.url for menu in new_menu: if menu['title'] == 'Home': menu['current'] = url.endswith('/') else: menu['current'] = url.endswith(menu['href']) return new_menu @view_config(renderer="templates/index.pt") def index_view(self): return {"page_title":

2022-05-03 15:31:09    分类:技术分享    python   pyramid

What does '@reify' do and when should it be used?

I saw it in the Pyramid tutorial for UX design. I couldn't make out much what this decorator is all about. Sample code where I saw its usage. def __init__(self, request): self.request = request renderer = get_renderer("templates/global_layout.pt") self.global_template = renderer.implementation().macros['layout'] @reify def company_name(self): return COMPANY @reify def site_menu(self): new_menu = SITE_MENU[:] url = self.request.url for menu in new_menu: if menu['title'] == 'Home': menu['current'] = url.endswith('/') else: menu['current'] = url.endswith(menu['href']) return new_menu @view_config

2022-05-01 00:57:11    分类:问答    python   pyramid

Passing form parameters between views in Pyramid

I am creating a form that requires user confirmation before submitting the data. I would like a seperate confirmation page because I need to display quite a bit information about how the form data will be processed. I was wondering if there was a pythonic way to pass data between forms in Pyramid. Submitting the form takes the user to the confirmation page. Thus, the view for the confirmation has the form data stored in request.POST. I was wondering if there was a clean way to pass along all of this data to the final view once the user hits 'submit' on the confirmation page. I would also like

2022-04-30 03:31:47    分类:问答    python   forms   pyramid

Writing doctests for pyramid web app which depend on settings in ini file

I would like to write doctests for my pyramid web app, using the webtest module. I tried it like this: from my_webapp import main from webtest import TestApp app = TestApp(main({})) result = app.get('/') This raises a KeyError (because some.url is not known) when my code reaches this line: url = request.registry.settings['some.url'] The value of some.url is specified in the paster ini file of my application. Is there a simple way to use my development.ini when running my test code? I did not yet fully understand how/when the ini file is loaded during pyramid start up, so it's hard to figure

2022-04-29 12:01:33    分类:问答    python   pyramid   wsgi   webtest

Check if a user has a permission in pyramid (pylons 2)?

How do i check if a user has a permission in pyramid. For example, I want to show some HTML only if a user has some permission, but have the view available for everybody.

2022-04-29 00:02:05    分类:问答    python   pylons   pyramid

python Requests login to website returns 403

I'm trying to use requests to login to a website but as you can guess I'm having a problem here's the the code that I'm using import requests EMAIL = '***' PASSWORD = '***' URL = 'https://portal.bitcasa.com/login' client = requests.session(config={'verbose': sys.stderr}) login_data = {'username': EMAIL, 'password': PASSWORD,} r = client.post(URL, data=login_data, headers={"Referer": "foo"}) print r and if I print out r.text I get <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html lang="en"> <head><script type="text/javascript">var

2022-04-28 08:02:32    分类:问答    python   django   pyramid   python-requests

Generic incoming file upload http request object for all python frameworks [closed]

问题 关闭。 这个问题需要更加集中。 它目前不接受答案。 想改进这个问题? 更新问题,使其仅通过编辑此帖子专注于一个问题。 5年前关闭。 改进这个问题 我正在开发一个 SDK,用于验证传入的 multipart/form-data 文件并将其保存到服务器端的磁盘。 就像是: sdk.upload(httpRequest, destinationPath, validationOptions) 我需要传入的 http 请求:字段名、文件名、文件和 mime 类型。 问题是python框架有不同的http请求对象: Django:HttpRequest request.FILES包含文件 Flask:request request.files包含文件。 金字塔: request.POST包含文件。 对于files字典中的文件值对象也是如此,它具有不同的结构。 我需要的是一个统一/通用的 http 请求对象或另一种以一种方式处理它的解决方案。 可能不方便的解决方案: 每个框架都有不同的实现:我想以通用的方式处理它。 使用 lib 解析 multipart/form-data:实际上是重新解析,因为框架已经解析过一次。 其他语言的例子: PHP:$_FILES 对象 NodeJS:可读流请求 ASP.NET:静态 HttpContext.Current.Request 回答1

2022-04-27 13:16:03    分类:技术分享    python   django   file-upload   flask   pyramid