通过 Web 浏览器将 pyodbc 与 MSSQL Server 集成的初始化配置问题

Initialization Configuration issues to integrate pyodbc with MSSQL Server via Web Browser

通过 Web 浏览器将 pyodbc 与 MSSQL Server 集成的初始化配置问题

因为我是 Python 网络编程的新手

我需要通过 单击提交按钮 进行编码,这应该 检索人员 [=47] 中的所有表 =]数据库到网络浏览器通过localhost.

运行 与本地主机的服务器配置应该是什么:

检查下面我配置的链接:

  1. 将 pyodbc 结果导入为 Apache HTTP 服务器中的内部服务器错误
  2. What should be my configuration to load pyodbc module on Apache Server to run simple queries mssql server database

我曾尝试使用 Apache HTTP Server 和一个命令 import pyodbc 结果 500 Internal Server Error。但是其余的事情在 POST 以及 Python Shell.

中都可以正常工作

我是这样编码的。请有人通过本地主机使用 Web 服务器让我知道我们需要什么 add/modify 使用以下代码

index.html

<form action="/cgi-bin/hello_get.py" method="post">
Click the submmit button to retrive all the tables
<input type="submit" value="Submit" />
</form>

hello_get.py

import pyodbc 
cnxn = pyodbc.connect("Driver={ODBC Driver 13 for SQL Server};"
                      "Server=DESKTOP-C6RS3DO;"
                      "Database=demo2016;"
                      "Trusted_Connection=yes;")

cursor = cnxn.cursor()
cursor.execute('SELECT * FROM person')

print("Content-Type: text/html")
print("")
print("<html><head>")
print("")
print("</head><body>")
for row in cursor:
    print('row = %r' % (row,))

print("Hello from Python.")
print("</body></html>")

cursor.close()
cnxn.close()

我需要上述代码中的任何额外模块和代码吗?

error.log

[cgi:error] [pid 9344:tid 1264] [client ::1:52332] AH01215:

End of script output before headers: hello_get.py, referer: http://localhost/index.html
Traceback (most recent call last):\r: C:/Apache24/htdocs/hello_get.py, referer: http://localhost/index.html
File "C:\Apache24\htdocs\hello_get.py", line 6, in <module>\r: C:/Apache24/htdocs/hello_get.py, referer: http://localhost/index.html
import pyodbc\r: C:/Apache24/htdocs/hello_get.py, referer: http://localhost/index.html
ImportError: No module named pyodbc\r: C:/Apache24/htdocs/hello_get.py, referer: http://localhost/index.html

但是每当尝试通过添加

httpd.config 加载模块时
 LoadModule pyodbc_module
 "c:/users/desktop/appdata/local/programs/python/python36-32/lib/site-packages/pyodbc.cp36-win32.pyd"

并重新启动服务器导致 httpd -k restart

httpd: Syntax error on line 576 of C:/Apache24/conf/httpd.conf: Can't locate API module structure `pyodbc_module' in file C:/Users/Vitriv-Desktop/AppData/Local/Programs/Python/Python36-32/Lib/site-packages/pyodbc.cp36-win32.pyd: No error

问题似乎是 Apache 找不到 Python 路径。不要忘记,Apache 在您的系统上以不同的用户身份运行。如果 Apache 以名为 www-user 的用户身份运行,则 www-user 需要能够导入 pyodbc.

如果您想坚持使用 CGI 而不是使用完整的 WSGI 应用程序,这里有两种可能的解决方案。

首先,在您的 Python 脚本中,您可以在任何导入语句之前执行此操作:

sys.path.insert(0, 'c:\path\to\python-site-packages\')

或者,在您的 Apache 配置中:

SetEnv PYTHONPATH "c:\path\to\python-site-packages\"

祝你好运!

在 Apache 中 500 内部错误已解决。因为导入pypyodbc.
在 python shell 中,我能够成功连接并在 python shell 以及 MSSQL 中检索结果服务器 2017


ex.py

    import sys
    sys.path.insert(0, 'C:/Users/Vitriv-Desktop/AppData/Local/Programs/Python/Python36-32/Lib/site-packages')
    import pypyodbc
    import cgi, cgitb

    # Create instance of FieldStorage
    form = cgi.FieldStorage()

    # Get data from fields
    first_name = form.getvalue('first_name')
    last_name  = form.getvalue('last_name')

    print("Content-Type:text/html\r\n\r\n")
    print("<html>")
    print("<head>")
    print("<title>Hello - Second CGI Program</title>")
    print("</head>")
    print("<body>")


    print("<h2>Hello %s %s</h2>" % (first_name, last_name)) 
    cnxn = pypyodbc.connect(driver='{ODBC Driver 13 for SQL Server}', server='DESKTOP-C6RS3DO', database='demo2016', Trusted_Connection='Yes')

    cursor = cnxn.cursor()
    cursor.execute('SELECT * FROM person')
    for row in cursor:
        print('row = %r' % (row,))
    print("<h2>Hello %s %s</h2>" % (first_name, last_name))   
    cursor.close()
    cnxn.close()

    print("</body>")
    print("</html>")

Index.html

<form action="ex.py" method="post">
First Name: <input type="text" name="first_name">  <br />

Last Name: <input type="text" name="last_name" />
<input type="submit" value="Submit" />
</form>