使用 mod_rewrite 隐藏在 Web 浏览器上访问的 Python 脚本的 .py 扩展名

发布于 2024-09-12 22:11:25 字数 2357 浏览 1 评论 0原文

我想隐藏 Web 浏览器中加载的 Python 脚本的 .py 扩展名,并且仍然让该脚本运行。例如:在地址栏中输入 url.dev/basics/pythonscript 会触发 pythonscript.py 并在浏览器窗口中显示结果。

  1. URL url.dev/basics/pythonscript 获取静态文件 /pythonscript.py
  2. 浏览器仍然显示 url url.dev/basics//pythonscript

输入 url.dev/basics/pythonscript.py 确实如此工作并显示 Python 脚本结果。我还可以使用 mod_rewrite 将 url.dev/basics/phpscript 重写为 url.dev/basics/phpscript.php 并在幕后成功运行 PHP 代码。

但是 url.dev/basics/pythonscript 不会重定向到 url.dev/basics/pythonscript.py (我收到 404 Not Found)

背景信息

A) PHP 重写有效:位于 url.dev/basics/ 的 .htaccess 中的以下内容适用于 PHP 脚本:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /basics/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
</IfModule>

B) Python 重写不起作用:位于 url.dev/basics/ 的 .htaccess 中的以下内容不适用于 Python 脚本(我收到 404 Not Found)

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /basics/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.py -f
RewriteRule ^(.*)$ $1.py
</IfModule>

C) 我是一名初级程序员,正在学习基础知识中的练习 2:您的第一个程序互联网应用软件工程部分。我正在尝试遵循建议,使用“一个 URL = 一个文件”的执行环境,但希望使用 Python 而不是 PHP。 我意识到这并不是构建 Web 应用程序的最佳方式。这只是在上面链接的课程的初始部分使用的约定。

D) 我在 OS 10.6 Snow Leopard 中设置了虚拟主机开发环境,以便我可以访问我的虚拟主机根据 adactio.com 上的“Hacky Holidays”在 url.dev 进行开发。我的Python版本是Python 2.6.1。

E) 我计划最终使用 Django,但如果可能的话,希望首先处理更简单的代码,以便我可以更好地理解正在发生的事情。

F) 我的 httpd.conf 中有以下内容:


TypesConfig /private/etc/apache2/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddHandler cgi-script .py

和:

LoadModule php5_module        libexec/apache2/libphp5.so
LoadModule fastcgi_module     libexec/apache2/mod_fastcgi.so

G) 我的 Apache 版本(重新启动服务器后在服务器日志中看到):Apache/2.2 .14 (Unix) mod_ssl/2.2.14 OpenSSL/0.9.8l DAV/2 PHP/5.3.1 mod_fastcgi/2.4.2 配置 -- 恢复正常操作

期待任何帮助!

I want to hide the .py extension of a Python script loaded in a web browser and still have the script run. For example: typing url.dev/basics/pythonscript in the address bar fires pythonscript.py and shows the results in the browser window.

  1. The URL url.dev/basics/pythonscript fetches the static file /pythonscript.py
  2. The browser still displays the url url.dev/basics//pythonscript

Typing in url.dev/basics/pythonscript.py DOES work and the Python script results is displayed. I can also get mod_rewrite to rewrite url.dev/basics/phpscript to url.dev/basics/phpscript.php and run the PHP code successfully behind the scenes.

But url.dev/basics/pythonscript does NOT redirect to url.dev/basics/pythonscript.py (I get a 404 Not Found).

Background Info

A) PHP rewriting works: the following in an .htaccess located in url.dev/basics/ WORKS for PHP scripts:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /basics/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
</IfModule>

B) Python rewriting does NOT work: the following in an .htaccess located in url.dev/basics/ does NOT work for Python scripts (I get a 404 Not Found):

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /basics/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.py -f
RewriteRule ^(.*)$ $1.py
</IfModule>

C) I am a beginning programmer working through Exercise 2: Your first program in the Basics section of Software Engineering for Internet Applications. I am trying to follow the recommendation to use an execution environment where 'One URL = one file', but want to use Python rather than PHP. I realize that this is not the best way to build a web application down the line. It is only a convention to be used during the initial part of the course linked above.

D) I set up the Virtual Hosts development environment in OS 10.6 Snow Leopard so that I can access my development at url.dev as per 'Hacky Holidays' at adactio.com. My Python version is Python 2.6.1.

E) I plan to use Django eventually, but want to work on simpler code first if possible so I can better understand what is going on.

F) I have the following in my httpd.conf:


TypesConfig /private/etc/apache2/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddHandler cgi-script .py

and:

LoadModule php5_module        libexec/apache2/libphp5.so
LoadModule fastcgi_module     libexec/apache2/mod_fastcgi.so

G) My Apache version (seen in server log after restarting the server): Apache/2.2.14 (Unix) mod_ssl/2.2.14 OpenSSL/0.9.8l DAV/2 PHP/5.3.1 mod_fastcgi/2.4.2 configured -- resuming normal operations

Looking forward to any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

琴流音 2024-09-19 22:11:25

如果您在 Linux 机器上运行,请不要使用 mod-rewrite - 重命名脚本。

您可以调用脚本 - pythonscript 而不是

您添加到脚本第一行的 pythonscript.py
指向你的Python解释器

将文件设置为可执行

-

chmod +x pythonscript 

并在文件执行时 它将读取文件的第一行
并在第一行执行解释器

#!/usr/bin/python 

,然后设置执行脚本的目录

以使其工作,您更改 .htaccess 文件中可执行目录中的文件

basics/.htaccess
-----------
Options +ExecCGI
SetHandler cgi-script
-----------

您的 python 脚本将如下所示

basics/pythonscript 
-------
#!/usr/bin/python

print "STATUS: 200 OK\n\n" 


print "hello world"
------

警告...您可能会收到一个error_log 文件中的错误如下所示。

[Thu Aug 05 19:26:34 2010] [alert] [client 127.0.0.1] /home/websites/testing/htdocs/basics/.htaccess: Option ExecCGI not allowed here

如果发生这种情况,您的网络服务器不允许更改您的 .htaccess 文件

您的网络服务器将需要能够允许更改您的 htaccess 文件
因此您可能需要在 httpd.conf 文件中启用Allowoverride All

<Directory "/">

.... 

Allowoverride All

</Directory>

If you are running on a linux box, dont use mod-rewrite - rename the script.

you can call the script - pythonscript not pythonscript.py

you add to the first line of the script
pointing to your python interpreter

and set the file to be executable

with

chmod +x pythonscript 

when the file is executed - it will read the first line of the file
and execute the interpeter in the first line

#!/usr/bin/python 

and then set the directory to execute the script

to make this work, you change the files in the directory executable in your .htaccess file

basics/.htaccess
-----------
Options +ExecCGI
SetHandler cgi-script
-----------

Your python script will look like this

basics/pythonscript 
-------
#!/usr/bin/python

print "STATUS: 200 OK\n\n" 


print "hello world"
------

Warning... You may get an error in your error_log file that looks like this.

[Thu Aug 05 19:26:34 2010] [alert] [client 127.0.0.1] /home/websites/testing/htdocs/basics/.htaccess: Option ExecCGI not allowed here

If that happens your webserver is not allowing the changes from your .htaccess file

Your webserver will need to be able to allow changes in your htaccess file
so you may need to enable Allowoverride All in your httpd.conf file

<Directory "/">

.... 

Allowoverride All

</Directory>
卸妝后依然美 2024-09-19 22:11:25

我找到了解决方案:我需要添加应用程序 MIME 类型,以便 Apache Web 服务器知道它应该运行代码。

在 url.dev/basics/.htaccess 添加:

<IfModule mime_module>
AddType application/x-httpd-py .py
</IfModule>

不要忘记重新启动 Apache: sudo apachectl Graceful

它可以工作了! http://url.dev/basics/pythonscript 运行pythonscript.py中的代码!请注意,我不确定这是否是最好的方法,但它确实有效。

这是我完整的 .htaccess 文件:

# Options +ExecCGI
# SetHandler cgi-script

<IfModule mime_module>
AddType application/x-httpd-py .py
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /basics/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.py -f
RewriteRule ^(.*)$ $1.py
</IfModule>

I found the solution: I needed to add the application MIME type so that the Apache Web Server understands it should run the code.

In url.dev/basics/.htaccess add:

<IfModule mime_module>
AddType application/x-httpd-py .py
</IfModule>

Don't forget to restart Apache: sudo apachectl graceful

And it works! http://url.dev/basics/pythonscript runs the code in pythonscript.py! Note that I'm not sure if this is the best way to do it, but it works.

Here is my complete .htaccess file:

# Options +ExecCGI
# SetHandler cgi-script

<IfModule mime_module>
AddType application/x-httpd-py .py
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /basics/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.py -f
RewriteRule ^(.*)$ $1.py
</IfModule>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文