cherrypy

cherrypy - URL dispatcher [duplicate]

烈酒焚心 提交于 2019-12-11 00:20:11
问题 This question already has an answer here : Closed 7 years ago . Possible Duplicate: Using mappings in CherryPy How would I map a url regEx such as /data/[A-Z].txt to a resource in cherrypy? Is there an simple example somewhere? I don't get the docs here. http://tools.cherrypy.org/wiki/RestfulDispatch 回答1: You could use a RoutesDispatcher import cherrypy class City: def __init__(self, name): self.name = name self.population = 10000 @cherrypy.expose def index(self, **kwargs): return "Welcome to

_cp_dispatch not getting called in cherrypy

泄露秘密 提交于 2019-12-10 19:46:36
问题 In the following example, I would expect to get an exception when accessing url http://127.0.0.1:8080/b/method_b. Instead, I get normal http response containing text 'method_b' in browser. No exception raised, meaning that _cp_dispatcher is not called. Am I getting something wrong about _cp_dispatch? I am using cherrypy version 3.8.0 in python 2.7.10 import cherrypy class B(object): def _cp_dispatch(self, vpath): raise Exception("Here!!") @cherrypy.expose def method_b(self): return "method_b"

Disable weak ciphers with cherrypy (python 2)

放肆的年华 提交于 2019-12-10 16:37:55
问题 I'm using Cherrypy 3.8.0 with Python 2 to use SSL/TLS using pyOpenSSL. I want to disable SSL3 to avoid POODLE (or other weak ciphers). Here's what I have so far: server_config={ 'server.socket_port': 443, 'server.ssl_module':'pyopenssl', 'server.ssl_certificate':'/path/myserver.crt', 'server.ssl_private_key':'/path/myserver.key', } This is similar to this question but for python 2 and pyopenssl. How can I specify or exclude specific ciphers? Thanks! 回答1: To disable SSL3, you should set the

Fix a 404: missing parameters error from a GET request to CherryPy

时光总嘲笑我的痴心妄想 提交于 2019-12-10 15:37:54
问题 I'm making a webpage using CherryPy for the server-side, HTML, CSS and jQuery on the client-side. I'm also using a mySQL database. I have a working form for users to sign up to the site - create a username and password. I use jQuery to send an AJAX POST request to the CherryPy which queries the database to see if that username exists. If the username exists, alert the user, if it doesn't, add it to the database and alert success. $.post('submit', postdata, function(data) { alert(data); });

Stop request processing in CherryPy and return 200 response from a tool

爷,独闯天下 提交于 2019-12-10 13:46:14
问题 My Question I am looking for a way to stop request processing in a Tool without raising an exception. In other words: I want to stop the request befor it gets to the specified controller and return a 2xx status code? Background We want our application to support CORS and therefore the preflight request. The idea was to write a tool which hooks before_handler . If an OPTIONS request is made, return the relevant CORS-headers and exit. The problem is, that I havn't found a way to stop the

sqlalchemy pymssql “connection reset by peer” recovery

不羁的心 提交于 2019-12-10 11:50:14
问题 I'm running a cherrypy webservice and wondering what the best option is to recover from "connection reset by peer" for a pymssql connection via sqlalchemy. Right now I have to restart the webservice. 回答1: This seems to be a bug in the is_disconnect() method for pymssql where it ignore TCP connection and timeout failures, leaving the cursor in an unhappy state; see http://www.sqlalchemy.org/trac/ticket/2172. For now, you can monkey-patch as: from sqlalchemy.dialects.mssql import pymssql def is

CherryPy REST Authentication

霸气de小男生 提交于 2019-12-10 11:35:21
问题 I am quite new to Python and CherryPy and trying to build a basic web application which will query data from the server with a RESTful API. I am trying to do it the right way from the start. One part I have not been able to figure out is authentication for the API as REST is supposed to be stateless, and you don't use sessions. I want to be able to use my API with "native clients" which does not have Cookies, so using session Cookies is not an option. The data will be accessed with AJAX in an

Getting Data back from Python with Cherrypy and jQuery/AJAX

和自甴很熟 提交于 2019-12-10 10:55:02
问题 Unfortunately i dont get ajax to work properly with cherrypy. Here is my python code: from mako.template import Template from mako.lookup import TemplateLookup import cherrypy import os import json CURDIR = os.getcwd() cherrypy.config.update({ "tools.staticdir.root" : CURDIR, "tools.staticdir.dir" : "static", "tools.staticdir.on" : True }) # Loopuoobjekt für die Templates tmplLookup = TemplateLookup(directories=['templates']) # Liefert das Gerenderte Template zurück def serve_template

Cannot serialize datetime as JSON from Cherrypy

亡梦爱人 提交于 2019-12-10 10:54:56
问题 I'm attempting to send a list of records in response to an Ajax query. This works well unless the results include a datetime field when my process fails with the error datetime.date(2011, 11, 1) is not JSON serializable . I attempted to combine the answer I found to a very similar question here with instructions in the CherryPy documentation to use a custom json_out encoder, but it's not clear to me what signature that function must have. The function I wrote is: def json_encoder(thing): if

How to return an image in an HTTP response with CherryPy

╄→尐↘猪︶ㄣ 提交于 2019-12-09 11:31:20
问题 I have code which generates a Cairo ImageSurface , and I expose it like so: def preview(...): surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) ... cherrypy.response.headers['Content-Type'] = "image/png" return surface.get_data() preview.exposed = True This doesn't work (browsers report that the image has errors). I've tested that surface.write_to_png('test.png') works, but I'm not sure what to dump the data into to return it. I'm guessing some file-like object? According to