0

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?

Nɪsʜᴀɴᴛʜ ॐ
  • 2,756
  • 4
  • 33
  • 57

1 Answers1

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

FlipperPA
  • 13,607
  • 4
  • 39
  • 71
  • [click here](https://stackoverflow.com/questions/47587961/by-adding-one-statement-import-pyodbc-causes-internal-server-error-in-apache-htt).I have followed your approach. Will you try to resolve atleast one method from the two methods – Nɪsʜᴀɴᴛʜ ॐ Dec 01 '17 at 06:43
  • Adding the above code will also causes 500 Internal Server Error because of import pyodbc print("Content-Type: text/html") print("") print("") print("") print("") for row in cursor: print('row = %r' % (row,)) print("") @FlipperPA – Nɪsʜᴀɴᴛʜ ॐ Dec 01 '17 at 09:57