Как запустить Python скрипт в Xampp

Скачиваем и устанавливаем Python
В Xampp Control Panel переходим в Config->Apache (httpd.conf)

apache config xampp

Порт сервера 80

ServerName localhost:80

Путь корневой директории, где находится сайт

DocumentRoot «C:/xampp/htdocs/»

Далее CGI Common Gateway Interface (интерфейс, позволяющий веб-серверам выполнять обработку запросов пользователей).

Options Indexes FollowSymLinks Includes ExecCGI

Здесь дописываем название главных страниц сайта с расширением .py

<IfModule dir_module>
DirectoryIndex index.php index.pl index.cgi index.asp index.shtml index.html index.htm \
default.php default.pl default.cgi default.asp default.shtml default.html default.htm \
home.php home.pl home.cgi home.asp home.shtml home.html home.htm index.py default.py home.py
</IfModule>

Вместо строки
AddHandler cgi-script .cgi .pl .asp
указываем
AddHandler cgi-script .cgi .pl .asp .py
ScriptInterpreterSource Registry-Strict

Далее находим строку <IfModule dir_module> и вместо

<IfModule dir_module>
DirectoryIndex index.php index.pl index.cgi index.asp index.shtml index.html index.htm \
default.php default.pl default.cgi default.asp default.shtml default.html default.htm \
home.php home.pl home.cgi home.asp home.shtml home.html home.htm
</IfModule>

Добавляем index.py

<IfModule dir_module>
DirectoryIndex index.php index.pl index.cgi index.asp index.shtml index.html index.htm \
default.php default.pl default.cgi default.asp default.shtml default.html default.htm \
home.php home.pl home.cgi home.asp home.shtml home.html home.htm index.py
</IfModule>

Сохраняем настройки и перезагружаем Xampp.
Для проверки работы  python скриптов в файле index.py вставляем скрипт

#!C:/Python27/python.exe
import time
print("Content-type: text/html;charset=utf-8\r\n\r\n")
print '&lt;html&gt;'
print '&lt;head&gt;'
print '&lt;title&gt;Hello Word - First CGI Program&lt;/title&gt;'
print '&lt;/head&gt;'
print '&lt;body&gt;'
a=2.0
b=3.0
c=b/a
print '2/3='
print c
print '&lt;br&gt;'
print time.asctime(time.localtime())
print '&lt;/body&gt;'
print '&lt;/html&gt;'

Данный скрипт выполнит операцию деления и отобразит текущее время

CGI Program


Если появится ошибка в браузере при запуске скрипта Internal Server Error

Internal Server Error

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at postmaster glocalhost to inform them of the time this error occurred, and the actions you performed just before this error. More information about this error may be available in the server error log.
Apache/2.4.46 (Win64) OpenSSUl. 1.Ih PHP/7.4.15 Server at localhost Port S

то смотрим логи в директории

C:\xampp\apache\logs

в файле

error.log

и видим ошибку

AH01215: SyntaxError: Non-ASCII character ‘\\xd1’ in file C:/xampp/htdocs/test/index.py on line 15, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details\r: C:/xampp/htdocs/test/index.py

ошибка связана с неправильной кодировкой, исправляем её и всё должно заработать

1199

Leave a Reply

Ваш адрес email не будет опубликован.