【问题标题】:mod_python and getting the QUERY_STRING using env_vars()mod_python 并使用 env_vars() 获取 QUERY_STRING
【发布时间】:2013-10-02 07:58:05
【问题描述】:

我在某些代码上遇到了一些问题,试图获取 QUERY_STRING,但是当我查看控制台中的错误时,它返回一个内部错误 500,这似乎是由 getReqStr = env_vars['QUERY_STRING'] 行引起的

任何人有任何想法如何解决这个问题吗?

import MySQLdb
import cgi, cgitb
from urlparse import urlparse

def index(req):

    req.add_common_vars()
    env_vars = req.subprocess_env
    getReqStr = env_vars['QUERY_STRING']           
    getReqArr = getReqStr.split('&')               
    getReqDict = {}

    for item in getReqArr:                          
       tempArr = item.split('=')                    
       getReqDict[tempArr[0]] = tempArr[1]
 
    dtbox = getReqDict['dt']
    tmbox = getReqDict['tm']

    con = MySQLdb.connect('localhost', 'root', '', 'mydb')

    with con:
        cur = con.cursor(MySQLdb.cursors.DictCursor)
        st = "SELECT tmp, watts FROM currentcost WHERE dt ='" + dtbox + "' and tm like '" + tmbox + "%'"
        cur.execute (s)
        rows = cur.fetchall()

        x=""
        y=""
        for row in rows:
            x=x+row["watts"]+","
            y=y+row["tmp"]+","

    x="data:["+x+"]"
    y="data:["+y+"]"

    con.close()

    req.write(st)

编辑。

这是我通过 Chrome 控制台返回的响应:

MOD_PYTHON ERRORProcessId: 3424Interpreter: '127.0.1.1'ServerName: '127.0.1.1'DocumentRoot: '/var/www'URI: '/currentcost.py'Location: NoneDirectory: '/var/www/'Filename: ' /var/www/currentcost.py'PathInfo: ''Phase: 'PythonHandler'Handler: 'mod_python.publisher' Traceback(最近一次调用最后):文件“/usr/lib/python2.7/dist-packages/mod_python/ importer.py”,第 1537 行,在 HandlerDispatch 默认=default_handler,arg=req,silent=hlist.silent)文件“/usr/lib/python2.7/dist-packages/mod_python/importer.py”,第 1229 行,在_process_target 结果 = _execute_target(config, req, object, arg) 文件“/usr/lib/python2.7/dist-packages/mod_python/importer.py”,第 1128 行,在 _execute_target 结果 = object(arg) 文件“/usr /lib/python2.7/dist-packages/mod_python/publisher.py”,第 204 行,在处理程序模块 = page_cache[req] 文件“/usr/lib/python2.7/dist-packages/mod_python/importer.py”中,第 1059 行,在 getitem中> 返回 import_module(req.filename) 文件“/usr/lib/python2.7/dist-packages/mod_python/importer.py”,第 296 行,在 import_module 日志中,import_path) 文件“/usr/lib/python2.7/ dist-packages/mod_python/importer.py”,第 680 行,在 import_module execfile(file, module.dict) 文件“/var/www/currentcost.py”,第 17 行 getReqStr = env_vars[' QUERY_STRING'] ' 后的网址? ^语法错误:扫描字符串时 EOL MODULE CACHE DETAILSAccessed:Thu Sep 26 09:18:30 2013Generation: 0_mp_545c0d0056a74a40503ad1da7dbb26e2 { FileName: '/var/www/currentcost.py' Instance: 1 [IMPORT] Generation: 0 [ERROR] Modified: Thu 2013 年 9 月 26 日 09:10:57}

【问题讨论】:

  • QUERY_STRING 甚至可能不存在于env_vars 中,这可能会引发KeyError 异常......我们可以有完整的回溯吗?
  • @Andre 我已经用完整的跟踪更新了帖子

标签: python query-string mod-python


【解决方案1】:

如果您使用的是mod_python,那么req.args 可能拥有您的所有数据。

您也可以使用req.parsed_uri[apache.URI_QUERY]获取query_string

如果您需要不假思索地处理 GET 和 POST,那么您最好使用与 mod_python 捆绑的 FieldStorage 类对其进行解析。应该是这样的:

from mod_python import util

getReqDict = util.FieldStorage(req)
dtbox = getReqDict['dt']
tmbox = getReqDict['tm']

作为对您代码的额外观察:您构建 SQL 查询的方式不安全并且容易受到SQL injection 的攻击。 在传递给 SQL 查询之前不要忘记对数据进行转义,或者改用query parameter binding(首选方法)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-22
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多