{"id":1531,"date":"2014-10-23T16:35:10","date_gmt":"2014-10-23T16:35:10","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=1531"},"modified":"2014-10-23T16:35:10","modified_gmt":"2014-10-23T16:35:10","slug":"web-py%e5%85%a5%e9%97%a8","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/1531.html","title":{"rendered":"Web.py\u5165\u95e8"},"content":{"rendered":"<ol>\n<li>\n<h5><strong>\u5b89\u88c5<\/strong><\/h5>\n<\/li>\n<\/ol>\n<p>\u5b89\u88c5web.py, \u8bf7\u5148\u4e0b\u8f7d\uff1a<\/p>\n<p><a href=\"http:\/\/webpy.org\/static\/web.py-0.37.tar.gz\">http:\/\/webpy.org\/static\/web.py-0.37.tar.gz<\/a><\/p>\n<p>\u6216\u8005\u83b7\u53d6\u6700\u65b0\u7684\u5f00\u53d1\u7248\uff1a<\/p>\n<p><a href=\"https:\/\/github.com\/webpy\/webpy\/tarball\/master\">https:\/\/github.com\/webpy\/webpy\/tarball\/master<\/a><\/p>\n<p>\u89e3\u538b\u5e76\u62f7\u8d1d\u00a0web\u00a0\u6587\u4ef6\u5939\u5230\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u76ee\u5f55\u4e0b\u3002<\/p>\n<p>\u6216\u8005\uff0c\u4e3a\u4e86\u8ba9\u6240\u6709\u7684\u5e94\u7528\u7a0b\u5e8f\u90fd\u53ef\u4ee5\u4f7f\u7528\uff0c\u8fd0\u884c\uff1a<\/p>\n<p>python setup.py install<\/p>\n<p>\u6ce8\u610f: \u5728\u67d0\u4e9b\u7c7bunix\u7cfb\u7edf\u4e0a\u4f60\u53ef\u80fd\u9700\u8981\u5207\u6362\u5230root\u7528\u6237\u6216\u8005\u8fd0\u884c\uff1a<\/p>\n<p>sudo python setup.py install<\/p>\n<p>\u67e5\u770b\uff1a<a href=\"http:\/\/webpy.org\/recommended_setup\" target=\"_blank\">\u63a8\u8350\u8bbe\u7f6e<\/a>\u3002<\/p>\n<p>\u53e6\u5916\u4e00\u4e2a\u9009\u62e9\u662f\u4f7f\u7528<a href=\"http:\/\/peak.telecommunity.com\/DevCenter\/EasyInstall\" target=\"_blank\">Easy Install<\/a>\u3002Easy Install \u4f7f\u7528\u5982\u4e0b\uff1a<\/p>\n<p>easy_install web.py<\/p>\n<p>\u6216\u8005\u00a0<a href=\"http:\/\/packages.python.org\/distribute\/\">PIP<\/a><\/p>\n<p>sudo pip install web.py<\/p>\n<ol start=\"2\">\n<li>\n<h5><strong>\u6837\u4f8b<\/strong><\/h5>\n<\/li>\n<\/ol>\n<p>web.py \u5185\u7f6e\u4e86web\u670d\u52a1\u5668\u3002\u53ef\u4ee5\u6309\u7167\u00a0<a href=\"http:\/\/webpy.org\/tutorial2\" target=\"_blank\">tutorial<\/a>\u00a0\u5b66\u4e60\u5982\u4f55\u5199\u4e00\u4e2aWeb\u5e94\u7528\u3002 \u5199\u5b8c\u540e\uff0c\u5c06\u4f60\u7684\u4ee3\u7801\u653e\u5230\u00a0code.py\u00a0\u6587\u4ef6\u4e2d\u5e76\u5982\u4e0b\u9762\u7684\u65b9\u6cd5\u6765\u542f\u52a8\u670d\u52a1\u5668\uff1a<\/p>\n<p>python code.py<\/p>\n<p>\u6253\u5f00\u4f60\u7684\u6d4f\u89c8\u5668\u8f93\u5165\u00a0<a href=\"http:\/\/localhost:8080\/\">http:\/\/localhost:8080\/<\/a>\u00a0\u67e5\u770b\u9875\u9762\u3002 \u82e5\u8981\u5236\u5b9a\u53e6\u5916\u7684\u7aef\u53e3\uff0c\u4f7f\u7528\u00a0python code.py 1234\u3002<\/p>\n<p>\u8fd9\u91cc\u6709\u4e00\u70b9\u662f\u8981\u6ce8\u610f\uff0c\u5982\u679c\u662f\u6839\u636e\u94fe\u63a5 <a href=\"http:\/\/webpy.org\/tutorial2\">http:\/\/webpy.org\/tutorial2<\/a> \u8fd9\u4e2a\u6587\u7ae0\u5199\u51fa\u7684\u4ee3\u7801\u662f\u8fd9\u4e2a\u6837\u5b50\u7684\uff1a<\/p>\n<pre class=\"lang:default decode:true\">import web\nurls = (\n\u00a0 '\/', 'index'\n)\nclass index:\n\u00a0\u00a0\u00a0 def GET(self):\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print \"Hello, world!\"\n\nif __name__ == \"__main__\": web.run(urls, globals())<\/pre>\n<p>\u8fd0\u884c\u8bdd\u4f1a\u62a5\u4e0b\u9762\u7684\u9519\u8bef\uff1a<\/p>\n<p>D:devwww&gt;python code.py<br \/>\nTraceback (most recent call last):<br \/>\nFile &#8220;code.py&#8221;, line 8, in &lt;module&gt;<br \/>\nif __name__ == &#8220;__main__&#8221;: web.run(urls, globals())<br \/>\n<span style=\"color: #ff0000;\"><strong>AttributeError: &#8216;module&#8217; object has no attribute &#8216;run&#8217;<\/strong><\/span><br \/>\n&lt;\/module&gt;<\/p>\n<p>\u6211\u4eec\u5e94\u8be5\u6839\u636e\u4e0b\u9762\u7684\u94fe\u63a5\u5199hello word <a href=\"http:\/\/webpy.org\/docs\/0.3\/tutorial\" target=\"_blank\">http:\/\/webpy.org\/docs\/0.3\/tutorial<\/a> \u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:default decode:true\">import web\nurls = (\n\u00a0\u00a0\u00a0 '\/', 'index'\n)\nclass index:\n\u00a0\u00a0\u00a0 def GET(self):\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return \"Hello, world!\"\nif __name__ == \"__main__\":\n\u00a0\u00a0\u00a0 app = web.application(urls, globals())\n\u00a0\u00a0\u00a0 app.run()<\/pre>\n<p>\u8fd0\u884c\uff1aD:devwww&gt;python code.py<\/p>\n<p>\u7136\u540e\u6d4f\u89c8\u5668\u8bbf\u95ee:http:\/\/127.0.0.1:8080\/\u00a0\u00a0\u00a0 \u770b\u5230hello, world!\u6211\u4eec\u5c31\u6210\u529f\u4e86\uff01<\/p>\n<p>\u7b2c\u4e00\u6b21\u914d\u7f6e\u7684\u65f6\u5019\uff0c\u4e0d\u77e5\u9053\u4f8b\u5b50\u4ee3\u7801\u8be5\u5982\u4f55\u5199\uff0c\u5199\u4e86\u4e2acode.py\u548cweb.py\uff0c\u7406\u6240\u5f53\u7136\u7684\u62a5\u9519\uff1b<\/p>\n<p>\u7b2c\u4e8c\u6b21\u76f4\u63a5\u590d\u5236\u4e0a\u9762\u7684\u4ee3\u7801\u5b58\u4e3aweb.py\u7136\u540e\u6267\u884c\u547d\u4ee4\u201cpython web.py\u201d\u62a5\u9519\u201cAttributeError: &#8216;module&#8217; object has no attribute &#8216;application&#8217;\u201d \u540e\u6765\u5728stackoverflow\u4e0a\u627e\u5230\u4e00\u70b9\u63d0\u793a\uff1a<span style=\"color: #ff0000;\"><strong>You are runing into name collisions.<\/strong><\/span> You named your package web, and are trying to import a module web. \u547d\u540d\u51b2\u7a81\u9519\u8bef\uff0c\u4e0d\u80fd\u5c06\u81ea\u5df1\u5199\u7684\u4ee3\u7801\u547d\u540d\u4e3aweb.py\u3002<\/p>\n<ol start=\"3\">\n<li>\n<h5><strong>\u4e00\u4e9b\u89e3\u91ca<\/strong><\/h5>\n<\/li>\n<\/ol>\n<h5>URL \u5904\u7406<\/h5>\n<p>\u4efb\u4f55\u4e00\u4e2a\u7f51\u7ad9\u6700\u91cd\u8981\u7684\u90e8\u5206\u90fd\u662f\u5b83\u7684 URL \u7ed3\u6784\uff0cURLs \u4e0d\u4ec5\u4ec5\u7528\u6765\u8ba9\u7f51\u7ad9\u7684\u8bbf\u95ee\u8005\u770b\u5230\u6216\u8005 email \u544a\u8bc9\u4ed6\u4eec\u7684\u670b\u53cb\uff0c\u5b83\u8fd8\u544a\u8bc9\u522b\u4eba\u4f60\u7684\u7f51\u7ad9\u662f\u6309\u7167\u600e\u6837\u7684\u601d\u7ef4\u6a21\u5f0f\u5de5\u4f5c\u7684\u3002\u50cf\u00a0<a href=\"http:\/\/del.icio.us\/\">del.icio.us<\/a>\u00a0\u8fd9\u6837\u7684\u7f51\u7ad9\uff0c\u5b83\u7684 URLs \u751a\u81f3\u662f\u7528\u6237\u754c\u9762\u7684\u4e00\u90e8\u5206\u3002web.py \u8ba9\u4f7f\u7528\u5f88\u68d2\u7684 URLs \u53d8\u5f97\u5f88\u5bb9\u6613\u3002<\/p>\n<p>\u4e0b\u9762\u5f00\u59cb\u5199\u57fa\u4e8e web.py \u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u65b0\u5efa\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\uff08\u8ba9\u6211\u4eec\u7ed9\u5b83\u8d77\u540d\u00a0code.py\u00a0\uff09\u5e76\u4e14\u8f93\u5165\uff1a<\/p>\n<pre class=\"lang:default decode:true\">import web<\/pre>\n<p>\u8fd9\u53e5\u8bdd\u7684\u4f5c\u7528\u662f\u5f15\u5165 web.py \u6a21\u5757\u3002<\/p>\n<p>\u73b0\u5728\u9700\u8981\u544a\u8bc9 web.py \u6211\u4eec\u7684 URL \u7ed3\u6784\u3002\u521a\u5f00\u59cb\u6211\u4eec\u5199\u4e2a\u7b80\u5355\u7684\uff1a<\/p>\n<pre class=\"lang:default decode:true\">urls = (\n  '\/', 'index'\n)<\/pre>\n<p><strong>\u7b2c\u4e00\u90e8\u5206<\/strong>\u662f\u4e00\u4e2a<a href=\"http:\/\/osteele.com\/tools\/rework\/\">\u6b63\u5219\u8868\u8fbe\u5f0f<\/a>\uff0c\u5b83\u7528\u6765\u5339\u914d\u4e00\u4e2a URL\uff0c\u4f8b\u5982\u00a0\/\uff0c\/help\/faq\uff0c\/item\/(d+)\u00a0\u7b49\u7b49\u3002\uff08d+\u00a0\u5339\u914d\u7684\u662f\u4e00\u4e32\u6570\u5b57\u3002\u62ec\u53f7\u8868\u793a\u5339\u914d\u4e0a\u90e8\u5206\u4f9b\u4ee5\u540e\u4f7f\u7528\uff09\uff0c<strong>\u7b2c\u4e8c\u90e8\u5206<\/strong>\u662f\u5904\u7406\u8bf7\u6c42\u7684\u7c7b\u7684\u540d\u5b57\uff0c\u4f8b\u5982\u00a0index\uff0cview\uff0cwelcome.hello\uff08welcomes\u00a0\u6a21\u5757\u7684\u00a0hello\u00a0\u7c7b\uff09\uff0c\u6216\u8005\u00a0get_1\u30021\u00a0\u7528\u6765\u88ab\u6b63\u5219\u8868\u8fbe\u5f0f\u6355\u6349\u5230\u7684\u7b2c\u4e00\u90e8\u5206\u66ff\u6362\uff0c\u6355\u6349\u5230\u7684\u5269\u4e0b\u7684\u90e8\u5206\u88ab\u4f20\u9012\u7ed9\u65b9\u6cd5\u3002<\/p>\n<p>\u8fd9\u4e00\u884c\u7684\u610f\u601d\u662f\u6211\u4eec\u628a URL \u662f\u00a0\/\uff08\u4f8b\u5982\u8bf4\u9996\u9875\uff09\u7684\u8bf7\u6c42\u4f20\u7ed9\u00a0index\u00a0\u7c7b\u6765\u5904\u7406\u3002<\/p>\n<p>\u73b0\u5728\u6211\u4eec\u9700\u8981\u5199\u00a0index\u00a0\u7c7b\u3002\u5927\u90e8\u5206\u4eba\u4e0a\u7f51\u53ea\u662f\u968f\u4fbf\u770b\u770b\uff0c\u4ed6\u4eec\u4e0d\u4f1a\u6ce8\u610f\u5230\u6d4f\u89c8\u5668\u7528\u7684\u662f\u4e00\u79cd\u88ab\u79f0\u4e3a HTTP \u7684\u8bed\u8a00\u4e0e World Wide Web \u8fdb\u884c\u4ea4\u6d41\u3002\u8fd9\u5176\u4e2d\u7684\u7ec6\u8282\u4e0d\u91cd\u8981\uff0c\u4f46\u662f\u6709\u4e00\u4e2a\u57fa\u672c\u7684\u6982\u5ff5\uff0c\u7f51\u7ad9\u8bbf\u95ee\u8005\u8ba9\u670d\u52a1\u5668\u6839\u636e URLs\uff08\u4f8b\u5982\u00a0\/\u00a0\u6216\u8005\u00a0\/foo?f=1\uff09\u5904\u7406\u76f8\u5e94\u7684\u52a8\u4f5c\uff08\u4f8b\u5982\u00a0GET\u6216\u00a0POST)\u3002<\/p>\n<p>GET\u00a0\u52a8\u4f5c\u6211\u4eec\u5f88\u719f\u6089\uff0c\u5b83\u88ab\u7528\u6765\u8bf7\u6c42\u4e00\u4e2a\u6587\u672c\u5f62\u5f0f\u7684\u7f51\u9875\u3002\u5f53\u4f60\u5728\u6d4f\u89c8\u5668\u5730\u5740\u680f\u8f93\u5165\u00a0harvard.edu\u00a0\u540e\uff0c\u5b83\u4f1a\u00a0GET\u00a0\u54c8\u4f5b\u5927\u5b66\u7f51\u7ad9\u670d\u52a1\u5668\u7684\/\u3002POST\u00a0\u52a8\u4f5c\u6211\u4eec\u4e5f\u4e0d\u964c\u751f\uff0c\u5b83\u7ecf\u5e38\u88ab\u7528\u6765\u63d0\u4ea4\u76f8\u5e94\u7684\u8868\u5355\uff0c\u4f8b\u5982\u4e00\u4e2a\u8d2d\u4e70\u4e1c\u4e1c\u7684\u8bf7\u6c42\u3002\u53ea\u8981\u4f60\u5728\u7f51\u4e0a\u505a\u4e00\u4e2a\u63d0\u4ea4\u8bf7\u6c42\u7684\u52a8\u4f5c\uff08\u6bd4\u65b9\u8bf4\u7528\u4fe1\u7528\u5361\u4e0b\u4e00\u4e2a\u5b9a\u5355\uff09\uff0c\u4f60\u5c31\u4f1a\u7528\u5230\u00a0POST\u3002\u4e86\u89e3\u8fd9\u4e24\u4e2a\u52a8\u4f5c\u7684\u4e0d\u540c\u4e4b\u5904\u5f88\u5173\u952e\uff0c\u56e0\u4e3a\u00a0GET\u00a0\u65b9\u5f0f\u7684\u00a0URLs\u00a0\u53ef\u4ee5\u88ab\u4f20\u9001\u548c\u88ab\u641c\u7d22\u5f15\u64ce\u7d22\u5f15\uff0c\u4f60\u4e00\u5b9a\u5e0c\u671b\u4f60\u7f51\u7ad9\u4e0a\u5927\u90e8\u5206\u7684\u7f51\u9875\u88ab\u7d22\u5f15\uff0c\u4f46\u662f\u4f60\u4e5f\u4e00\u5b9a<em>\u4e0d\u5e0c\u671b<\/em>\u6b63\u5728\u5904\u7406\u7684\u5b9a\u5355\u88ab\u7d22\u5f15\uff08\u60f3\u8c61\u4e00\u4e0b\u5982\u679c Google \u8bd5\u56fe\u8d2d\u4e70\u4f60\u7f51\u7ad9\u4e0a\u6240\u6709\u7684\u4e1c\u4e1c\uff01\uff09\u3002<\/p>\n<p>\u5728 web.py \u4ee3\u7801\u91cc\uff0c\u6211\u4eec\u628a\u8fd9\u4e24\u4e2a\u52a8\u4f5c\u533a\u522b\u5f97\u5f88\u6e05\u695a\uff1a<\/p>\n<pre class=\"lang:default decode:true\">class index:\n    def GET(self):\n        print \"Hello, world!\"<\/pre>\n<p>\u53ea\u8981\u6709\u4eba\u63d0\u7ed9\u670d\u52a1\u5668\u4e00\u4e2a URL \u4e3a\u00a0\/\u00a0\u7684\u00a0GET\u00a0\u8bf7\u6c42\uff0c\u4e0a\u9762\u7684\u00a0GET\u00a0\u65b9\u6cd5\u5c06\u88ab web.py \u8c03\u7528\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u73b0\u5728\u6211\u4eec\u9700\u8981\u5b8c\u6210\u7a0b\u5e8f\u7684\u6700\u540e\u4e00\u884c\uff0c\u544a\u8bc9 web.py \u5f00\u59cb\u63d0\u4f9b\u7f51\u9875\u670d\u52a1\uff1a<\/p>\n<pre class=\"lang:default decode:true\">if __name__ == \"__main__\": web.run(urls, globals())<\/pre>\n<p>\u8fd9\u53e5\u8bdd\u662f\u8ba9 web.py \u5728\u8fd9\u4e2a\u6587\u4ef6\uff08code.py\uff09\u7684\u5168\u5c40\u540d\u79f0\u7a7a\u95f4\u91cc\u67e5\u627e\u76f8\u5e94\u7684\u7c7b\u7ed9\u4e0a\u9762\u5217\u51fa\u7684 URLs \u63d0\u4f9b\u76f8\u5e94\u7684\u670d\u52a1\u3002<\/p>\n<p>\u73b0\u5728\u8bf7\u6ce8\u610f\uff0c\u5c3d\u7ba1\u6211\u5728\u8fd9\u513f\u53fd\u53fd\u6b6a\u6b6a\u8bb2\u4e86\u4e00\u5927\u5806\uff0c\u53ef\u662f\u6211\u4eec\u5b9e\u9645\u4e0a\u5c31\u5199\u4e86 5 \u884c\u4ee3\u7801\u3002\u8fd9\u5c31\u662f\u6211\u4eec\u5199\u4e00\u4e2a\u5b8c\u6574\u7684 web.py \u5e94\u7528\u7a0b\u5e8f\u6240\u9700\u8981\u7684\u5168\u90e8\u3002\u5982\u679c\u4f60\u53bb\u547d\u4ee4\u884c\u7a97\u53e3\u8f93\u5165\uff1a<\/p>\n<p>$ python code.pyLaunching server: http:\/\/0.0.0.0:8080\/<\/p>\n<p>\u73b0\u5728\u4f60\u7684 web.py \u5e94\u7528\u7a0b\u5e8f\u5c31\u8fd0\u884c\u5728\u4e00\u4e2a\u771f\u5b9e\u7684 web server \u4e2d\u4e86\u3002\u8bbf\u95ee\u90a3\u4e2a URL\uff08http:\/\/localhost:8080\uff09\uff0c\u4f60\u5e94\u8be5\u770b\u5230\u201cHello, world!\u201d\uff08\u4f60\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u7684 code.py \u540e\u9762\u6dfb\u52a0 IP \u5730\u5740\/\u7aef\u53e3\u6765\u63a7\u5236 web.py \u542f\u52a8 server \u7684\u4f4d\u7f6e\u3002\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u00a0fastcgi\u00a0\u6216\u8005\u00a0scgi\u00a0\u53c2\u6570\u6765\u8fd0\u884c\u76f8\u5e94\u7c7b\u578b\u7684\u670d\u52a1\u3002\uff09<\/p>\n<h5>\u6a21\u7248<\/h5>\n<p>\u5728 Python \u91cc\u9762\u5199 HTML \u662f\u975e\u5e38\u8ba9\u4eba\u538c\u5026\u7684\uff1b\u800c\u5728 HTML \u91cc\u9762\u5199 Python \u5219\u8981\u6709\u610f\u601d\u7684\u591a\u3002\u5e78\u8fd0\u7684\u662f\uff0cweb.py \u8ba9\u8fd9\u4e00\u5207\u53d8\u5f97\u5f88\u7b80\u5355\u3002<\/p>\n<p><strong>\u8ba9\u6211\u4eec\u4e3a\u6a21\u7248\u65b0\u5efa\u4e00\u4e2a\u76ee\u5f55\uff08\u76ee\u5f55\u540d\u662ftemplates<\/strong><strong>\uff09<\/strong>\u3002\u6211\u4eec\u518d\u5728\u8fd9\u4e2a\u76ee\u5f55\u91cc\u65b0\u5efa\u4e00\u4e2a\u6269\u5c55\u540d\u662f html \u7684\u6587\u4ef6\uff08\u6211\u4eec\u53eb\u5b83\u00a0index.html\uff09\u3002\u73b0\u5728\uff0c\u4f60\u53ef\u4ee5\u5728\u8fd9\u4e2a\u6587\u4ef6\u91cc\u5199\u666e\u901a\u7684 HTML\uff1a<\/p>\n<pre class=\"lang:default decode:true\">&lt;em&gt;Hello&lt;\/em&gt;, world!<\/pre>\n<p>\u6216\u8005\u4f60\u53ef\u4ee5\u7528 web.py \u7684\u6a21\u7248\u8bed\u8a00\u5f80 html \u6587\u4ef6\u91cc\u6dfb\u52a0\u4ee3\u7801\uff1a<\/p>\n<pre class=\"lang:default decode:true\">$def with (name)\n\n$if name:\n    I just wanted to say &lt;em&gt;hello&lt;\/em&gt; to $name.\n$else:\n    &lt;em&gt;Hello&lt;\/em&gt;, world!<\/pre>\n<p>\u6ce8\u610f\uff1a\u73b0\u5728\u4ee3\u7801\u91cc\u9700\u8981\u6709 4 \u4e2a\u7a7a\u683c\u7684\u7f29\u8fdb\u3002<\/p>\n<p>\u6b63\u5982\u4f60\u770b\u5230\u7684\uff0c\u9664\u4e86<strong>\u9876\u90e8\u6709<\/strong><strong>\u00a0<\/strong><strong>def with<\/strong><strong>\u00a0<\/strong><strong>\u7684\u58f0\u660e\uff08<\/strong><strong>saying what the template gets called with<\/strong><strong>\uff09<\/strong>\u4ee5\u53ca<strong>\u6bcf\u884c\u4ee3\u7801\u5f00\u5934\u90fd\u6709<\/strong><strong>\u00a0<\/strong><strong>$<\/strong><strong>\u00a0<\/strong>\u5916\uff0c\u8fd9\u4e2a\u6a21\u7248\u6709\u70b9\u50cf Python \u6587\u4ef6\u3002\u76ee\u524d\uff0c<strong>template.py <\/strong><strong>\u8981\u6c42<\/strong><strong> $def <\/strong><strong>\u58f0\u660e\u5728\u6587\u4ef6\u7684\u7b2c\u4e00\u884c\u3002<\/strong>\u800c\u4e14\u8bf7\u6ce8\u610f\uff0cweb.py \u4f1a\u81ea\u52a8\u8f6c\u4e49\u5728\u8fd9\u513f\u4f7f\u7528\u7684\u4efb\u4f55\u53d8\u91cf\uff0c\u8fd9\u6837\uff0c\u5982\u679c\u4e3a\u4e86\u67d0\u79cd\u9700\u8981\uff0cname\u00a0\u88ab\u8d4b\u6210\u4e00\u4e2a\u5305\u542b HTML \u7684\u503c\uff0c\u5b83\u5c06\u5f97\u5230\u6b63\u786e\u5730\u8f6c\u4e49\u5e76\u4f5c\u4e3a\u5e73\u6587\u672c\u663e\u793a\u51fa\u6765\u3002\u5982\u679c\u4f60\u60f3\u5173\u6389\u8fd9\u4e2a\u529f\u80fd\uff0c\u7528$:name\u00a0\u4ee3\u66ff\u00a0$name\u3002<\/p>\n<p>\u73b0\u5728\u56de\u5230\u00a0code.py\uff0c\u5728\u7b2c\u4e00\u884c\u4e0b\u9762\u52a0\u4e0a\uff1a<\/p>\n<pre class=\"lang:default decode:true \">render = web.template.render('templates\/')<\/pre>\n<p>\u8fd9\u540d\u8bdd\u7684\u610f\u601d\u662f\u544a\u8bc9 web.py \u5728 templates \u76ee\u5f55\u4e2d\u5bfb\u627e\u6a21\u7248\u6587\u4ef6\u3002\u7136\u540e\u628a\u00a0index.GET\u00a0\u6539\u6210\uff1a<\/p>\n<pre class=\"lang:default decode:true \">name = 'Bob'print render.index(name)<\/pre>\n<p>\u8bbf\u95ee\u4f60\u7684\u7ad9\u70b9\uff0c\u5b83\u5e94\u8be5\u53d8\u4e3a say hello to Bob \u4e86\u3002<\/p>\n<p><strong>\u5c0f\u6280\u5de7<\/strong>\uff1a\u628a\u00a0cache=False\u00a0\u52a0\u5728\u00a0render\u00a0\u7684\u8bed\u53e5\u540e\u9762\uff0c\u8fd9\u6837\u4f60\u6bcf\u6b21\u8bbf\u95ee\u9875\u9762\u65f6 web.py \u90fd\u4f1a\u91cd\u65b0\u52a0\u8f7d\u6a21\u7248\u6587\u4ef6\u3002<\/p>\n<p>\u73b0\u5728\u6539\u53d8 URL \u90a3\u4e00\u884c\u8bed\u53e5\u4e3a\uff1a<\/p>\n<pre class=\"lang:default decode:true \">'\/(.*)', 'index'<\/pre>\n<p>\u63a5\u7740\u6539\u00a0index.GET\u00a0\u7684\u5b9a\u4e49\u4e3a\uff1a<\/p>\n<p>def GET(self, name):<\/p>\n<p>\u7136\u540e\u5220\u9664\u7ed9 name \u8d4b\u503c\u7684\u8bed\u53e5\u3002\u8bbf\u95ee\u00a0\/\u00a0\uff0c\u9875\u9762\u5e94\u8be5\u662f say hello to the world\u3002\u8bbf\u95ee\u00a0\/Joe\u00a0\uff0c\u9875\u9762\u5e94\u8be5\u662f say hello to Joe\u3002<\/p>\n<h2>\u6570\u636e\u5e93\u4ea4\u4e92<\/h2>\n<p>\u5728\u00a0web.run\u00a0\u8fd9\u4e00\u53e5\u4e4b\u4e0a\u6dfb\u52a0\uff1a<\/p>\n<pre class=\"lang:default decode:true\">web.config.db_parameters = dict(dbn='postgres', user='username', pw='password', db='dbname')<\/pre>\n<p>\uff08\u6309\u7167\u4f60\u81ea\u5df2\u7684\u8bbe\u7f6e\u8c03\u6574\u8fd9\u4e9b\u53c2\u6570\u2014\u2014\u7279\u522b\u662f\u00a0username\uff0cpassword\u00a0\u548c\u00a0dbname\uff0c<strong>MySQL <\/strong><strong>\u7684\u7528\u6237\u9700\u8981\u628a<\/strong><strong>\u00a0<\/strong><strong>dbn<\/strong><strong>\u00a0<\/strong><strong>\u7684\u503c\u6539\u6210<\/strong><strong>\u00a0<\/strong><strong>mysql<\/strong><strong>\u3002\u8fd8\u6709\u5c31\u662f<\/strong><strong>host<\/strong><strong>\uff0c\u5bf9\u4e8e<\/strong><strong>MySQLdb<\/strong><strong>\u6765\u8bf4\uff0c\u4f7f\u7528<\/strong><strong>localhost<\/strong><strong>\u53ef\u80fd\u4f1a\u62a5\u9519\uff0c\u9700\u8981\u624b\u52a8\u4fee\u6539\u4e3a<\/strong><strong>127.0.0.1<\/strong>\uff09<\/p>\n<p>\u5728\u4f60\u7684\u6570\u636e\u5e93\u91cc\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u8868\uff1a<\/p>\n<pre class=\"lang:default decode:true\">CREATE TABLE todo (\n  id serial primary key,\n  title text,\n  created timestamp default now(),\n  done boolean default 'f'\n);<\/pre>\n<p>\u5e76\u4e14\u6dfb\u52a0\u4e00\u6761\u521d\u59cb\u7684\u6761\u76ee\uff1a<\/p>\n<pre class=\"lang:default decode:true\">INSERT INTO todo (title) VALUES ('Learn web.py');<\/pre>\n<p>\u56de\u5230\u00a0code.py\u00a0\uff0c\u6539\u53d8\u00a0index.GET\u00a0\u4e3a\uff1a<\/p>\n<pre class=\"lang:default decode:true\">def GET(self):\n    todos = web.select('todo')\n    print render.index(todos)<\/pre>\n<p>\u518d\u628a\u5904\u7406 URL \u7684\u8bed\u53e5\u6539\u56de\u6210\u00a0\/\u3002<\/p>\n<p>\u6309\u4e0b\u9762\u4ee3\u7801\u7f16\u8f91\u00a0index.html\uff1a<\/p>\n<pre class=\"lang:default decode:true\">$def with (todos)\n&lt;ul&gt;\n$for todo in todos:\n    &lt;li id=\"t$todo.id\"&gt;$todo.title&lt;\/li&gt;\n&lt;\/ul&gt;<\/pre>\n<p>\u8bbf\u95ee\u4f60\u7684\u7ad9\u70b9\uff0c\u4f60\u5e94\u8be5\u53ef\u4ee5\u770b\u5230\u4e00\u6761 todo \u9879\u76ee\uff1a\u201cLearn web.py\u201d\u3002\u606d\u559c\uff01\u4f60\u5df2\u7ecf\u6709\u4e00\u4e2a\u8bfb\u6570\u636e\u5e93\u7684\u5b8c\u6574\u7684\u5e94\u7528\u7a0b\u5e8f\u4e86\u3002<\/p>\n<p>\u5728\u00a0index.html\u00a0\u6587\u4ef6\u672b\u5c3e\u6dfb\u52a0\uff1a<\/p>\n<pre class=\"lang:default decode:true\">&lt;form method=\"post\" action=\"add\"&gt;\n&lt;p&gt;&lt;input type=\"text\" name=\"title\" \/&gt; &lt;input type=\"submit\" value=\"Add\" \/&gt;&lt;\/p&gt;\n&lt;\/form&gt;<\/pre>\n<p>\u7136\u540e\u6539\u4f60\u7684 URLs \u5217\u8868\u4e3a\uff1a<\/p>\n<pre class=\"lang:default decode:true\">'\/', 'index',\n'\/add', 'add'<\/pre>\n<p>\uff08<strong>\u5bf9\u9017\u53f7\u7684\u4f7f\u7528\u4f60\u8981\u975e\u5e38\u5c0f\u5fc3\u3002\u5982\u679c\u4f60\u5ffd\u7565\u5b83\u4eec\uff0c<\/strong><strong>Python <\/strong><strong>\u5c06\u628a\u8fd9\u4e9b\u5b57\u7b26\u4e32\u5408\u5728\u4e00\u8d77<\/strong>\uff0c\u4f60\u7684 URLs \u5217\u8868\u5c06\u53d8\u6210&#8217;\/index\/addadd&#8217;\uff01\uff09<\/p>\n<p>\u73b0\u5728\u518d\u5199\u4e00\u4e2a\u7c7b\uff1a<\/p>\n<pre class=\"lang:default decode:true\">class add:\n    def POST(self):\n        i = web.input()\n        n = web.insert('todo', title=i.title)\n        web.seeother('.\/#t'+str(n))<\/pre>\n<p>\uff08\u6ce8\u610f\u5728\u8fd9\u91cc\u6211\u4eec\u662f\u600e\u6837\u5e94\u7528\u00a0POST\u00a0\u7684\uff1f\uff09<\/p>\n<p>web.input\u00a0\u8ba9\u4f60\u5f97\u5230\u7528\u6237\u901a\u8fc7\u63d0\u4ea4\u8868\u5355\u4f20\u9012\u7ed9\u4f60\u7684\u6240\u6709\u53d8\u91cf\u3002web.insert\u00a0\u628a\u503c\u63d2\u5165\u5230\u6570\u636e\u5e93\u4e2d\u7684\u00a0todo\u00a0\u8868\u4e2d\u5e76\u4e14\u8fd4\u56de\u65b0\u6761\u76ee\u7684 ID \u3002seeother\u91cd\u5b9a\u5411\u7528\u6237\u5230\u8fd9\u4e2a ID\u3002<\/p>\n<p>\u901f\u89c8\uff1aweb.transact()\u00a0\u5f00\u542f\u4e00\u4e2a\u4e8b\u52a1\uff08transaction\uff09\u3002web.commit()\u00a0\u63d0\u4ea4\u8fd9\u4e2a\u4e8b\u52a1\uff1bweb.rollback()\u00a0\u56de\u6eda\u8fd9\u4e2a\u4e8b\u52a1\u3002web.update\u00a0\u8ddfweb.insert\u00a0\u5dee\u4e0d\u591a\uff0c\u4f46\u662f\u5b83\u4e0d\u662f\u8fd4\u56de ID \u800c\u662f it takes it\uff08\u6216\u8005\u4e00\u4e2a WHERE \u5b50\u53e5\uff09after the table name.<\/p>\n<p>web.input\uff0cweb.query\u00a0\u548c\u00a0web.py\u00a0\u4e2d\u7684\u5176\u5b83\u4e00\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u8fd4\u56de\u201c\u5b58\u50a8\u5bf9\u8c61\uff08Storage objects\uff09\u201d\uff0c\u5b83\u5c31\u50cf\u5b57\u5178\uff08dictionaries\uff09\uff0c\u4f60\u53ef\u4ee5\u5199d.foo\u00a0\u4e5f\u53ef\u4ee5\u5199\u00a0d[&#8216;foo&#8217;]\u3002\u8fd9\u786e\u5b9e\u4f7f\u4ee3\u7801\u53d8\u5f97\u5e72\u51c0\u6574\u6d01\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u5728<a href=\"http:\/\/webpy.infogami.com\/docs\" target=\"_blank\">\u8fd9\u7bc7\u6587\u6863<\/a>\u91cc\u627e\u5230\u5173\u4e8e\u8fd9\u4e9b\u7684\u5168\u90e8\u7ec6\u8282\u548c web.py \u7684\u6240\u6709\u65b9\u6cd5\u3002<\/p>\n<p>\u73b0\u5728\u6559\u7a0b\u7ed3\u675f\u4e86\u3002\u9605\u8bfb<a href=\"http:\/\/webpy.infogami.com\/docs\" target=\"_blank\">\u8fd9\u7bc7\u6587\u6863<\/a>\u5427\uff0c\u4f60\u53ef\u4ee5\u7528 web.py \u505a\u66f4\u591a\u66f4\u9177\u7684\u4e8b\u60c5\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u53c2\u8003\u94fe\u63a5\uff1a<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/webpy.org\/docs\/0.3\/tutorial.zh-cn\" target=\"_blank\">web.py 0.3 \u65b0\u624b\u6307\u5357 (web.py)<\/a><\/li>\n<li><a href=\"http:\/\/webpy.org\/src\/\" target=\"_blank\">Code samples (web.py)<\/a><\/li>\n<li><a href=\"http:\/\/blog.csdn.net\/caleng\/article\/details\/5712850\" target=\"_blank\">web.py \u5341\u5206\u949f\u521b\u5efa\u7b80\u6613\u535a\u5ba2<\/a><\/li>\n<li><a href=\"http:\/\/simple-is-better.com\/news\/309\" target=\"_blank\">Simple-TODO: \u4e00\u4e2a\u7b80\u6613\u7684 todo \u7a0b\u5e8f &#8211; web.py \u4e2d\u6587\u6559\u7a0b<\/a><\/li>\n<li><a href=\"http:\/\/www.leavesongs.com\/PYTHON\/webpy-readme.html\" target=\"_blank\">web.py\u6307\u5357\u6027\u8bf4\u660e<\/a><\/li>\n<li><a href=\"http:\/\/www.leavesongs.com\/PENETRATION\/web-py-runcode-tip.html\" target=\"_blank\">web.py\u4f7f\u7528\u4e0d\u5f53\u53ef\u80fd\u9020\u6210\u4ee3\u7801\u6267\u884c<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5b89\u88c5 \u5b89\u88c5web.py, \u8bf7\u5148\u4e0b\u8f7d\uff1a http:\/\/webpy.org\/static\/web.py-0.37. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[381],"class_list":["post-1531","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","tag-web-py"],"views":2468,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1531","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=1531"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1531\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=1531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=1531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=1531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}