0

我有一个脚本:

#!/usr/local/bin/python

import cgi
import pymysql as MySQLdb
import os,sys,traceback
import cgitb
from xml.dom.minidom import Document
import site
from distutils.sysconfig import get_python_lib
import distutils.sysconfig


print "Content-type: text/html"
print

print os.environ['PYTHONPATH'].split(os.pathsep)
print os.environ['PATH'].split(os.pathsep)
print distutils.sysconfig.get_python_lib()
print os.sep.join([sys.prefix, 'lib', 'python' + sys.version[:3], 'site-packages'])
print(get_python_lib())
print sys.version
print sys.prefix
print MySQLdb.__dict__
print "Test complete"
print MySQLdb.VERSION

当我执行它时,我得到:

curl "http://box.com/cgi-bin/first.py"

['/usr/local/lib/python2.7/site-packages']
['/usr/local/bin/', '/usr/kerberos/sbin', '/usr/kerberos/bin', '/usr/local/bin', '/bin', '/usr/bin', '/usr/X11R6/bin']
/usr/local/lib/python2.7/site-packages
/usr/local/lib/python2.7/site-packages
/usr/local/lib/python2.7/site-packages
2.7.3 (default, Jun  6 2012, 10:53:07)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)]
/usr/local
{'__name__': 'pymysql', '__file__': '/usr/local/lib/python2.7/site-packages/pymysql', '__path__': ['/usr/local/lib/python2.7/site-packages/pymysql'], '__doc__': None, '__package__': None}
Test complete

注意 print MySQLdb.VERSION 值没有被打印出来。现在,当我在命令行中使用相同的程序时:

bin:>/usr/local/bin/python

Python 2.7.3 (default, Jun  6 2012, 10:53:07)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cgi
>>> import pymysql as MySQLdb
>>> import os,sys,traceback
>>> import cgitb
>>> from xml.dom.minidom import Document
>>> import site
>>> from distutils.sysconfig import get_python_lib
>>> import distutils.sysconfig
>>> print "Content-type: text/html"
Content-type: text/html
>>> print

>>>
>>> print os.environ['PYTHONPATH'].split(os.pathsep)
['/usr/local/lib/python2.7/site-packages']
>>> print os.environ['PATH'].split(os.pathsep)
['/usr/kerberos/sbin', '/usr/kerberos/bin', '/usr/local/bin', '/bin', '/usr/bin', '/usr/X11R6/bin']
>>> print distutils.sysconfig.get_python_lib()
/usr/local/lib/python2.7/site-packages
>>> print os.sep.join([sys.prefix, 'lib', 'python' + sys.version[:3], 'site-packages'])
/usr/local/lib/python2.7/site-packages
>>> print(get_python_lib())
/usr/local/lib/python2.7/site-packages
>>> print sys.version
2.7.3 (default, Jun  6 2012, 10:53:07)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)]
>>> print sys.prefix
/usr/local
>>> print MySQLdb.__dict__
{'DBAPISet': <class 'pymysql.DBAPISet'>, 'FIELD_TYPE': <module 'pymysql.constants.FIELD_TYPE' from '/usr/local/lib/python2.7/site-packages/pymysql/constants/FIELD_TYPE.pyc'>, 'Binary': <function Binary at 0x2b2ba7b805f0>, 'InternalError': <class 'pymysql.err.InternalError'>, 'Time': <type 'datetime.time'>, 'NUMBER': DBAPISet([0, 1, 3, 4, 5, 8, 9, 13]), '__path__': ['/usr/local/lib/python2.7/site-packages/pymysql'], 'connect': <function Connect at 0x2b2ba7b807d0>, 'escape_dict': <function escape_dict at 0x2b2ba70a1140>, 'DATE': DBAPISet([10, 14]), 'escape_string': <function escape_string at 0x2b2ba70a1410>, 'times': <module 'pymysql.times' from '/usr/local/lib/python2.7/site-packages/pymysql/times.pyc'>, 'MySQLError': <class 'pymysql.err.MySQLError'>, 'threadsafety': 1, 'NULL': 'NULL', 'thread_safe': <function thread_safe at 0x2b2ba7b808c0>, 'TimeFromTicks': <function TimeFromTicks at 0x2b2ba7b80500>, 'Timestamp': <type 'datetime.datetime'>, 'charset': <module 'pymysql.charset' from '/usr/local/lib/python2.7/site-packages/pymysql/charset.pyc'>, '__package__': 'pymysql', 'Date': <type 'datetime.date'>, 'ProgrammingError': <class 'pymysql.err.ProgrammingError'>, 'TimestampFromTicks': <function TimestampFromTicks at 0x2b2ba7b80578>, 'converters': <module 'pymysql.converters' from '/usr/local/lib/python2.7/site-packages/pymysql/converters.pyc'>, 'Connect': <function Connect at 0x2b2ba7b807d0>, 'NotSupportedError': <class 'pymysql.err.NotSupportedError'>, 'DataError': <class 'pymysql.err.DataError'>, 'STRING': DBAPISet([253, 254, 247]), 'version_info': (1, 2, 2, 'final', 0), '__builtins__': {'bytearray': <type 'bytearray'>, 'IndexError': <type 'exceptions.IndexError'>, 'all': <built-in function all>, 'help': Type help() for interactive help, or help(object) for help about object., 'vars': <built-in function vars>, 'SyntaxError': <type 'exceptions.SyntaxError'>, 'unicode': <type 'unicode'>, 'UnicodeDecodeError': <type 'exceptions.UnicodeDecodeError'>, 'memoryview': <type 'memoryview'>, 'isinstance': <built-in function isinstance>, 'copyright': Copyright (c) 2001-2012 Python Software Foundation.
All Rights Reserved.

Copyright (c) 2000 BeOpen.com.
All Rights Reserved.

Copyright (c) 1995-2001 Corporation for National Research Initiatives.
All Rights Reserved.

Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.
All Rights Reserved., 'NameError': <type 'exceptions.NameError'>, 'BytesWarning': <type 'exceptions.BytesWarning'>, 'dict': <type 'dict'>, 'input': <built-in function input>, 'oct': <built-in function oct>, 'bin': <built-in function bin>, 'SystemExit': <type 'exceptions.SystemExit'>, 'StandardError': <type 'exceptions.StandardError'>, 'format': <built-in function format>, 'repr': <built-in function repr>, 'sorted': <built-in function sorted>, 'False': False, 'RuntimeWarning': <type 'exceptions.RuntimeWarning'>, 'list': <type 'list'>, 'iter': <built-in function iter>, 'reload': <built-in function reload>, 'Warning': <type 'exceptions.Warning'>, '__package__': None, 'round': <built-in function round>, 'dir': <built-in function dir>, 'cmp': <built-in function cmp>, 'set': <type 'set'>, 'bytes': <type 'str'>, 'reduce': <built-in function reduce>, 'intern': <built-in function intern>, 'issubclass': <built-in function issubclass>, 'Ellipsis': Ellipsis, 'EOFError': <type 'exceptions.EOFError'>, 'locals': <built-in function locals>, 'BufferError': <type 'exceptions.BufferError'>, 'slice': <type 'slice'>, 'FloatingPointError': <type 'exceptions.FloatingPointError'>, 'sum': <built-in function sum>, 'getattr': <built-in function getattr>, 'abs': <built-in function abs>, 'exit': Use exit() or Ctrl-D (i.e. EOF) to exit, 'print': <built-in function print>, 'True': True, 'FutureWarning': <type 'exceptions.FutureWarning'>, 'ImportWarning': <type 'exceptions.ImportWarning'>, 'None': None, 'hash': <built-in function hash>, 'ReferenceError': <type 'exceptions.ReferenceError'>, 'len': <built-in function len>, 'credits':     Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands
    for supporting Python development.  See www.python.org for more information., 'frozenset': <type 'frozenset'>, '__name__': '__builtin__', 'ord': <built-in function ord>, 'super': <type 'super'>, 'TypeError': <type 'exceptions.TypeError'>, 'license': Type license() to see the full license text, 'KeyboardInterrupt': <type 'exceptions.KeyboardInterrupt'>, 'UserWarning': <type 'exceptions.UserWarning'>, 'filter': <built-in function filter>, 'range': <built-in function range>, 'staticmethod': <type 'staticmethod'>, 'SystemError': <type 'exceptions.SystemError'>, 'BaseException': <type 'exceptions.BaseException'>, 'pow': <built-in function pow>, 'RuntimeError': <type 'exceptions.RuntimeError'>, 'float': <type 'float'>, 'MemoryError': <type 'exceptions.MemoryError'>, 'StopIteration': <type 'exceptions.StopIteration'>, 'globals': <built-in function globals>, 'divmod': <built-in function divmod>, 'enumerate': <type 'enumerate'>, 'apply': <built-in function apply>, 'LookupError': <type 'exceptions.LookupError'>, 'open': <built-in function open>, 'quit': Use quit() or Ctrl-D (i.e. EOF) to exit, 'basestring': <type 'basestring'>, 'UnicodeError': <type 'exceptions.UnicodeError'>, 'zip': <built-in function zip>, 'hex': <built-in function hex>, 'long': <type 'long'>, 'next': <built-in function next>, 'ImportError': <type 'exceptions.ImportError'>, 'chr': <built-in function chr>, 'xrange': <type 'xrange'>, 'type': <type 'type'>, '__doc__': "Built-in functions, exceptions, and other objects.\n\nNoteworthy: None is the `nil' object; Ellipsis represents `...' in slices.", 'Exception': <type 'exceptions.Exception'>, 'tuple': <type 'tuple'>, 'UnicodeTranslateError': <type 'exceptions.UnicodeTranslateError'>, 'reversed': <type 'reversed'>, 'UnicodeEncodeError': <type 'exceptions.UnicodeEncodeError'>, 'IOError': <type 'exceptions.IOError'>, 'hasattr': <built-in function hasattr>, 'delattr': <built-in function delattr>, 'setattr': <built-in function setattr>, 'raw_input': <built-in function raw_input>, 'SyntaxWarning': <type 'exceptions.SyntaxWarning'>, 'compile': <built-in function compile>, 'ArithmeticError': <type 'exceptions.ArithmeticError'>, 'str': <type 'str'>, 'property': <type 'property'>, 'GeneratorExit': <type 'exceptions.GeneratorExit'>, 'int': <type 'int'>, '__import__': <built-in function __import__>, 'KeyError': <type 'exceptions.KeyError'>, 'coerce': <built-in function coerce>, 'PendingDeprecationWarning': <type 'exceptions.PendingDeprecationWarning'>, 'file': <type 'file'>, 'EnvironmentError': <type 'exceptions.EnvironmentError'>, 'unichr': <built-in function unichr>, 'id': <built-in function id>, 'OSError': <type 'exceptions.OSError'>, 'DeprecationWarning': <type 'exceptions.DeprecationWarning'>, 'min': <built-in function min>, 'UnicodeWarning': <type 'exceptions.UnicodeWarning'>, 'execfile': <built-in function execfile>, 'any': <built-in function any>, 'complex': <type 'complex'>, 'bool': <type 'bool'>, 'ValueError': <type 'exceptions.ValueError'>, 'NotImplemented': NotImplemented, 'map': <built-in function map>, 'buffer': <type 'buffer'>, 'max': <built-in function max>, 'object': <type 'object'>, 'TabError': <type 'exceptions.TabError'>, 'callable': <built-in function callable>, 'ZeroDivisionError': <type 'exceptions.ZeroDivisionError'>, 'eval': <built-in function eval>, '__debug__': True, 'IndentationError': <type 'exceptions.IndentationError'>, 'AssertionError': <type 'exceptions.AssertionError'>, 'classmethod': <type 'classmethod'>, 'UnboundLocalError': <type 'exceptions.UnboundLocalError'>, 'NotImplementedError': <type 'exceptions.NotImplementedError'>, 'AttributeError': <type 'exceptions.AttributeError'>, 'OverflowError': <type 'exceptions.OverflowError'>}, '__file__': '/usr/local/lib/python2.7/site-packages/pymysql/__init__.pyc', 'IntegrityError': <class 'pymysql.err.IntegrityError'>, 'escape_sequence': <function escape_sequence at 0x2b2ba70a11b8>, 'DATETIME': DBAPISet([12, 7]), 'sys': <module 'sys' (built-in)>, 'Connection': <function Connect at 0x2b2ba7b807d0>, 'VERSION': (0, 5, None), 'Error': <class 'pymysql.err.Error'>, '__name__': 'pymysql', 'install_as_MySQLdb': <function install_as_MySQLdb at 0x2b2ba7b80938>, 'constants': <module 'pymysql.constants' from '/usr/local/lib/python2.7/site-packages/pymysql/constants/__init__.pyc'>, 'DateFromTicks': <function DateFromTicks at 0x2b2ba7b80488>, 'BINARY': DBAPISet([249, 250, 251, 252]), '__version__': '0.5.None', '__all__': ['BINARY', 'Binary', 'Connect', 'Connection', 'DATE', 'Date', 'Time', 'Timestamp', 'DateFromTicks', 'TimeFromTicks', 'TimestampFromTicks', 'DataError', 'DatabaseError', 'Error', 'FIELD_TYPE', 'IntegrityError', 'InterfaceError', 'InternalError', 'MySQLError', 'NULL', 'NUMBER', 'NotSupportedError', 'DBAPISet', 'OperationalError', 'ProgrammingError', 'ROWID', 'STRING', 'TIME', 'TIMESTAMP', 'Warning', 'apilevel', 'connect', 'connections', 'constants', 'converters', 'cursors', 'escape_dict', 'escape_sequence', 'escape_string', 'get_client_info', 'paramstyle', 'threadsafety', 'version_info', 'install_as_MySQLdb', 'NULL', '__version__'], 'err': <module 'pymysql.err' from '/usr/local/lib/python2.7/site-packages/pymysql/err.pyc'>, 'InterfaceError': <class 'pymysql.err.InterfaceError'>, 'paramstyle': 'format', 'TIMESTAMP': DBAPISet([12, 7]), 'get_client_info': <function get_client_info at 0x2b2ba7b80848>, '__doc__': '\nPyMySQL: A pure-Python drop-in replacement for MySQLdb.\n\nCopyright (c) 2010 PyMySQL contributors\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the "Software"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n\n', 'Warning': <class 'pymysql.err.Warning'>, 'OperationalError': <class 'pymysql.err.OperationalError'>, 'ROWID': DBAPISet([]), 'TIME': DBAPISet([11]), 'apilevel': '2.0', 'DatabaseError': <class 'pymysql.err.DatabaseError'>}
>>> print "Test complete"
Test complete
>>> print MySQLdb.VERSION
(0, 5, None)
>>>

我不确定我应该做些什么额外的事情才能使 curl 调用(就像通过浏览器一样)也可以工作。问题是当我运行时它会出错:

curl "http://box.com/cgi-bin/first.py"

AttributeError: 'module' object has no attribute 'VERSION'

解决了!!!!

问题是 /usr/local/lib/python2.7/site-packages/pymysql 及其子目录中的文件确实具有执行权限。将它们更改为 755(全部)后,问题已解决。

4

0 回答 0