What should be my configuration to load pyodbc module on Apache Server to run simple queries mssql server database

China☆狼群 提交于 2019-12-02 16:31:41

问题


As I'm new to python.

I need to know simple database connectivity with Apache HTTP Server.

Just I need to run the below code in Apache HTTP Server.

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

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


for row in cursor:
    print('row = %r' % (row,))

cursor.close();
cnxn.close();

I have tried to run in python shell. It executed successfully.
But with Apache HTTP Server results in 500 Internal Server Error.

Also in httpd.conf file:

 LoadModule pyodbc_module "c:/users/desktop/appdata/local/programs/python/python36-32/lib/site-packages/pyodbc.cp36-win32.pyd"

Results

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

So are there any modules or code that should be imported/modified to run with Apache?


回答1:


You're missing several layers to your stack, which are really good ideas to learn and use if you want to leverage pyodbc:

  • Apache (installed) mod_wsgi (WSGI is a specification that describes how a web server communicates with web applications)
  • A framework to protect you from very bad things happening

However, if all you want is for that to appear on a web page, you can do it as a CGI program. This is a bad idea, but is quick and dirty.

You'll need to add this before any output (before for row):

print("Content-Type: text/html;charset=utf-8")
print()

And then follow these instructions to configure Apache to run your Python script: https://www.linux.com/blog/configuring-apache2-run-python-scripts



来源:https://stackoverflow.com/questions/47573523/what-should-be-my-configuration-to-load-pyodbc-module-on-apache-server-to-run-si

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!