python - CherryPy: Perform specific action if user goes to specific URL -
i'm new cherrypy please bear me. i'd perform specific action when user goes specific url. url same except 1 part. url like: http://myserver.mydomain.com/show_item.cgi?id=12345. url same except 12345. want take "string of numbers", plop them variable, , re-direct url built on fly based on variable. have logic building out url -- don't know how "intercept" incoming url , extract "string of numbers". help appreciated.
oh, not clear question wanted mock cgi-like file handler url. though reply is still there, may harder find because of recent changes in documentation.
you can utilize dots in uri /path/to/my.html
, python method names don’t allow dots. work around this, default dispatcher converts dots in uri underscores before trying find page handler. in example, therefore, name page handler def my_html
.
so next can navigate browser http://127.0.0.1:8080/show_item.cgi?id=1234
.
#!/usr/bin/env python # -*- coding: utf-8 -*- import cherrypy config = { 'global' : { 'server.socket_host' : '127.0.0.1', 'server.socket_port' : 8080, 'server.thread_pool' : 8 } } class app: @cherrypy.expose def show_item_cgi(self, id): raise cherrypy.httpredirect('https://google.com/search?q={0:d}'.format(int(id))) if __name__ == '__main__': cherrypy.quickstart(app(), '/', config)
python cherrypy
No comments:
Post a Comment