Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

225
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda