免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 5779 | 回复: 11
打印 上一主题 下一主题

(求助)python3 web 传递参数 返回 数据库 查询结果 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-12-19 22:30 |只看该作者 |倒序浏览
[i=s] 本帖最后由 Chrome 于 2010-12-19 22:42 编辑 [/i]

我是新手!

我的运行环境 python3.12  win xp SP3 iis5+.py

我的想法: 通过 input.html  输入一些数字,传递到 result.py 中,result.py把输入的数字在数据库中查询出来,反馈到页面上。



=========input.thml============= code:

<html>
Please input the number:
<p>
<div class="top w960 center">
<form action="result.py" method="post">
number: <input type="text" name="txtWords"><br>
<input type="submit">
</div>
</form>
</html>

==========result.py============code:
#coding=utf-8
import cx_Oracle,os,sys,http.client,urllib.parse,urllib.request
words=request.post['txtWords']
conn = cx_Oracle.connect('wahaha','123456','18.97.27.6:1521/chrome')  
cursor = conn.cursor()
sql = "select staff_number,cash,key_status,valid_time from staff_info where staff_number ='"+words+"'"
cursor.execute(sql)
for r in cursor.fetchall():print ("number: %s  cash: %s  status: %s  valid: %s " % (r[0], r[1],r[2],r[3]))

===========================================
[color=Blue]代码执行以后,每次都是返回

Traceback (most recent call last):
  File "D:\Soft\Python31\web\result.py", line 3, in
    words=request.post['txtWords']
NameError: name 'request' is not defined[/color]

论坛徽章:
0
2 [报告]
发表于 2010-12-19 22:32 |只看该作者
我想自己写,暂时不想用其他的页面模版。

谢谢各位路过的,如果有遇到过这类问题,请推荐一本书或文章给我看也行。谢谢!

论坛徽章:
0
3 [报告]
发表于 2010-12-21 09:46 |只看该作者
难道没人知道?

论坛徽章:
0
4 [报告]
发表于 2010-12-21 10:22 |只看该作者
>>> import urllib.request
>>> f = urllib.request.urlopen('http://www.python.org/')
>>> print(f.read(300))
http://docs.python.org/dev/library/urllib.request.html#examples

这是很基本的问题,自己找一本Python的基础教程好好看一下吧

论坛徽章:
0
5 [报告]
发表于 2010-12-21 10:46 |只看该作者
本帖最后由 Chrome 于 2010-12-21 10:54 编辑
>>> import urllib.request
>>> f = urllib.request.urlopen('http://www.python.org/')
>>> print(f.rea ...
protagonist 发表于 2010-12-21 10:22



谢谢你的耐心回复。我需要的传递数据给result.py,然后查询数据库出结果,不是抓取网页内容的。能否根据我提供的一楼代码,帮我看看代码该怎么改啊?

谢谢!

论坛徽章:
0
6 [报告]
发表于 2010-12-21 10:57 |只看该作者
或者你给推荐一本书看也行。

论坛徽章:
0
7 [报告]
发表于 2010-12-21 17:48 |只看该作者

论坛徽章:
0
8 [报告]
发表于 2010-12-22 01:53 |只看该作者
回复 1# Chrome

req是mod_python里的一个方法,你在action里指定了result.py后面是post的意思是action里的post方法。
对应的result.py内容大概应该是如下
from mod_python import apache
def post(req):
    req.content_type='text/plain'
    req.write("Hello, world!")
    return apache.OK

python没有request方法,对应的是req
url里的参数对应的是req.args   
可能有误,具体你再核实一下。反正架子大概是这么个关系。你照着这个关系改一下你的程序应该就能跑了。

论坛徽章:
0
9 [报告]
发表于 2010-12-22 02:49 |只看该作者
  1. #!/usr/bin/env python
  2. # -*- coding=utf-8 -*-

  3. import cgi, cgitb

  4. class CGI:
  5.         def __init__(self):
  6.                 cgitb.enable()

  7.         def cgiInit(self):
  8.                 print "Content-type: text/html\r\n"

  9.                 self.post = cgi.FieldStorage()
  10.                 print self.post

  11. def main():
  12.         _cgi = CGI()
  13.         _cgi.cgiInit()

  14. if __name__ == "__main__":
  15.         main()
复制代码

论坛徽章:
0
10 [报告]
发表于 2010-12-22 14:33 |只看该作者
本帖最后由 Chrome 于 2010-12-22 14:44 编辑
lvdbing 发表于 2010-12-22 02:49



谢谢 lvdbing ,下面是我在PYTON3 里面修改好的,可以执行的代码:
  1. # -*- coding=utf-8 -*-

  2. import cgi, cgitb

  3. class CGI:
  4.         def __init__(self):
  5.                 cgitb.enable()

  6.         def cgiInit(self):
  7.                 print ("Content-type: text/html\r\n")

  8.                 self.post = cgi.FieldStorage()
  9.                 print (self.post)

  10. def main():
  11.         _cgi = CGI()
  12.         _cgi.cgiInit()

  13. if __name__ == "__main__":
  14.         main()
复制代码

上面是我执行成功后,返回的页面。
请问,我怎么才能把图片中的数字,传递到SQL语句中。
谢谢热心的 lvdbing 。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP