Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

227
Views
pymysql don't running in python cgi

I am using CentOs 7 and alread installed Apache2. I try to run a cgi script with Python 3. I have a file named index.py with the following code:

#!/usr/bin/env python3.7

import platform
import pymysql
import cgitb
cgitb.enable()

print ("Content-type: text/html\r\n\r\n")
print ("<html>\n<body>")

print ("<div style=\"width: 100%; font-size: 40px; font-weight: bold; text-align: center;\$

connection = pymysql.connect(host='127.0.0.1', user='user', password='password', db='database', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)


try:
       with connection.cursor() as cursor:
                sql = "SELECT * FROM Marken"
                cursor.execute(sql)
                rows = cursor.fetchall()
                for row in rows:
                        print(row['ID'], row['markenName'], '</br>')
finally:
        connection.close()


print ("Python Script Test Page", platform.python_version())
print ("</div>\n</body>\n</html>")

which gives a 500 internal server error.

I added following lines to apache httpd.conf:

<Directory "/var/www/html/cgi">
    Options +ExecCGI
    AddHandler cgi-script .py
</Directory>

and made the file acessable throut sudo chmod 755 var/www/html/cgi/index.py

So if I comment out all the pymysql code and test it in browser it prints out the expected Python Test Page text and the python platform version 3.7.4. I can not even import pymysql as it gives an server error. With the mysql.connector I had the same problem. If I run the script as a standalone script with the pymysql code it works alright in shell.

I configure my system as a non root user with sudo access. Is this the problem? and are there maybe any enviroment variables to be set?

What I'm doing wrong?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

<VirtualHost *:80>

    <Directory "/var/www/html/cgi">

        Options +ExecCGI

        DirectoryIndex index.py

    </Directory>

    AddHandler cgi-script .py

    DocumentRoot /var/www/html/cgi

</VirtualHost>

in apache configuration section you missed to define directory index for python file. also define DocumentRoot so python index file should able point to correct path. based on documentation you need to rid out handler from directory.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!