简体   繁体   English

Python-在CGI脚本中导入MySQLdb在浏览器中不起作用

[英]Python - import MySQLdb in a CGI script not working in a browser

I'm having some trouble with MySQLdb when using it in a CGI script. 在CGI脚本中使用MySQLdb时遇到麻烦。 I'm on OSx and running off a local host. 我在OSx上并在本地主机上运行。

Basically, when I run my code from the terminal it runs fine, but when I run it from a browser, it executes everything up until the import MySQLdb line, and nothing after. 基本上,当我从终端运行代码时,它运行良好,但是当我从浏览器中运行代码时,它会执行所有操作,直到导入MySQLdb行,然后才执行。

For example: 例如:

#!/usr/bin/python 
print "Content-Type: text/html\n" 
print "abc"

In the browser and the terminal, this prints out abc. 在浏览器和终端中,这将输出abc。

But the following: 但是以下内容:

#!/usr/bin/python 
print "Content-Type: text/html\n" 
print "abc"
import MySQLdb 
print "123"

In the terminal prints out abc and 123, but in the browser it just prints out abc. 在终端中打印出abc和123,但在浏览器中仅打印出abc。

It seems that it's having a problem with the import MySQLdb line, but I can't see any errors. 似乎导入MySQLdb行有问题,但是我看不到任何错误。

Would anyone have any ideas? 有人有什么想法吗?

To narrow down the problem, I'd look in two places. 为了缩小问题的范围,我将在两个地方进行研究。 The first would be your webserver's error log. 第一个是您的网络服务器的错误日志。 I'm not sure where that is on OS X, but on eg Red Hat it can be found in /var/log/httpd/error_log . 我不确定OS X上的哪个位置,但是在Red Hat上可以在/var/log/httpd/error_log找到它。 Use tail -f to watch the error log as you submit your HTTP request: 提交HTTP请求时,使用tail -f查看错误日志:

$ tail -f /var/log/httpd/error_log

The other thing you can look into is Python's cgi and cgitb modules. 您可以研究的另一件事是Python的cgicgitb模块。 The former will save you having to reinvent wheels, and the latter will give you tracebacks in your browser. 前者将节省您重新设计轮子的麻烦,而后者将使您在浏览器中具有追溯功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM