如何在PHP-Apache-PostgreSQL站点中嵌入python脚本?

时间:2022-03-11 07:15:40

I have a internet site built in PHP-Apache-PostgreSQL; i built myself a python script that implement an algorithm of image comparison. It returns most similar images taken from the db.

我有一个建立在PHP-Apache-PostgreSQL中的网站;我为自己构建了一个python脚本,它实现了一个图像比较算法。它返回从数据库中获取的最相似的图像。

Now, i have to create a webpage where i embed my python script. My question is: Which is the fastest and easy solution?

现在,我必须创建一个网页,将python脚本嵌入其中。我的问题是:哪一个是最快、最简单的解决方案?

  1. Create a php page and communicate with the python script?
  2. 创建一个php页面并与python脚本通信?
  3. Install WSGI framework for Apache and create a python web page with my python script?
  4. 为Apache安装WSGI框架并使用我的python脚本创建一个python web页面?
  5. install WSGI+ Django and create a python web page with my pythonscript?
  6. 安装WSGI+ Django并使用我的pythonscript创建一个python web页面?
  7. using a socket?
  8. 使用套接字?

I tried the first 2 options. in a php page I had difficulty in communicating variables with my python script Using WSGI, i have to change my way of thinking: in php logic and interface are mixed, in python are divided.

我尝试了前两个选项。在一个php页面中,我在使用WSGI与python脚本通信时遇到了困难,我必须改变我的思维方式:在php逻辑和接口是混合的,在python中是分开的。

UPDATE: My python script implements an algorithm of image comparison. It returns most similar images taken from the db.

更新:我的python脚本实现了一种图像比较算法。它返回从数据库中获取的最相似的图像。

1 个解决方案

#1


1  

Without being completely aware of what you python script is doing, I will try to give you a quick run through of what I would do in your place.

在不完全了解您的python脚本正在做什么的情况下,我将尝试快速介绍我在您的位置上将做什么。

I use Django for all web exposed applications. It works great to customize the url patterns, access to legacy data is superb and the orb for interacting with these data covers 99% of my needs in all projects.

我对所有web公开的应用程序都使用Django。定制url模式非常有用,对遗留数据的访问非常出色,与这些数据交互的orb满足了我在所有项目中99%的需求。

Setting up Django to run in apache is explained in details in the Django documentation. Once you have your new site up and running side by side with your php app you can start the communication between them.

在Django文档中详细介绍了如何设置要在apache中运行的Django。一旦你有了新的网站,并与你的php应用程序并排运行,你就可以开始他们之间的交流了。

Django accessing the PHP data

Django访问PHP数据

Django manage.py can create a model for you based on existing tables. In some cases you can get an out of the box model file ready to use, but in most cases you need to go in an create your own order and customization of relation between you tables. Setup the database section of your settings.py to point to your PHP app database.

Django管理。py可以根据现有的表为您创建一个模型。在某些情况下,您可以使用现成的模型文件,但是在大多数情况下,您需要创建自己的顺序并定制表之间的关系。设置设置的数据库部分。py指向PHP应用程序数据库。

DATABASES = {
'default': {
    'ENGINE':'django.db.backends.postgresql_psycopg2',
    'NAME': 'myphpappdbname',
    'USER': 'phpuser',
    'PASSWORD': 'phppassword'
    # snipped away other options. Might or might not bee needed
}

}

}

Once you have your setup you can test access to your data with

一旦设置好了,就可以用它来测试对数据的访问了

python manage.py dbshell

When you have ensured correct connection to the database you can run

当您确保到数据库的正确连接后,您就可以运行了

python manage.py inspectdb > models.py

python管理。py inspectdb > models.py

This will generate a model for each of your tables. Add this model file to and app in your new django project.

这将为每个表生成一个模型。在新的django项目中添加这个模型文件和应用程序。

python manage.py startapp phpextension
mv models.py phpextension/models.py

Modify your installed apps in settings to include your newly installed app and you now have access to your whole PHP app data through your django app. Create your views for what data you wish expose on the web.

在设置中修改已安装的应用程序以包含新安装的应用程序,现在您可以通过django应用程序访问整个PHP应用程序数据。

As an added note, if you with to camouflage to the public that you are mixing php and django/python you can create your URL patterns with the following:

作为附加说明,如果您想要将php和django/python混合在一起,您可以使用以下方法创建URL模式:

url(r'^my_command.php$', 'phpextensions.views.my_command', name="my_command"),

For this to work I think you also need to set the setting APPEND_SLASH=False.

为此,我认为您还需要设置设置APPEND_SLASH=False。

The views will then see the django view as just another php script (only faster, more gracious and infused with magical unicorns ).

这些视图将看到django视图仅仅是另一个php脚本(只是速度更快,更亲切,并且充满了魔法独角兽)。

Access to your Django data through PHP

通过PHP访问Django数据

Here I would use a REST api. The simple way is just to build a JSON response in your view with json.dumps(my_dictionary_of_data_for_php) and the more enterprise way is to use tastypie.

这里我将使用REST api。简单的方法是在您的视图中使用JSON .dumps(my_dictionary_of_data_for_php)构建一个JSON响应,更企业级的方法是使用taste ypie。

Then you can access the data in PHP using

然后可以使用PHP访问数据

$fc = file_get_contents('http://yourdomain.com/path/to/django/my_command.php');
$django_data = json_decode(fc);

#1


1  

Without being completely aware of what you python script is doing, I will try to give you a quick run through of what I would do in your place.

在不完全了解您的python脚本正在做什么的情况下,我将尝试快速介绍我在您的位置上将做什么。

I use Django for all web exposed applications. It works great to customize the url patterns, access to legacy data is superb and the orb for interacting with these data covers 99% of my needs in all projects.

我对所有web公开的应用程序都使用Django。定制url模式非常有用,对遗留数据的访问非常出色,与这些数据交互的orb满足了我在所有项目中99%的需求。

Setting up Django to run in apache is explained in details in the Django documentation. Once you have your new site up and running side by side with your php app you can start the communication between them.

在Django文档中详细介绍了如何设置要在apache中运行的Django。一旦你有了新的网站,并与你的php应用程序并排运行,你就可以开始他们之间的交流了。

Django accessing the PHP data

Django访问PHP数据

Django manage.py can create a model for you based on existing tables. In some cases you can get an out of the box model file ready to use, but in most cases you need to go in an create your own order and customization of relation between you tables. Setup the database section of your settings.py to point to your PHP app database.

Django管理。py可以根据现有的表为您创建一个模型。在某些情况下,您可以使用现成的模型文件,但是在大多数情况下,您需要创建自己的顺序并定制表之间的关系。设置设置的数据库部分。py指向PHP应用程序数据库。

DATABASES = {
'default': {
    'ENGINE':'django.db.backends.postgresql_psycopg2',
    'NAME': 'myphpappdbname',
    'USER': 'phpuser',
    'PASSWORD': 'phppassword'
    # snipped away other options. Might or might not bee needed
}

}

}

Once you have your setup you can test access to your data with

一旦设置好了,就可以用它来测试对数据的访问了

python manage.py dbshell

When you have ensured correct connection to the database you can run

当您确保到数据库的正确连接后,您就可以运行了

python manage.py inspectdb > models.py

python管理。py inspectdb > models.py

This will generate a model for each of your tables. Add this model file to and app in your new django project.

这将为每个表生成一个模型。在新的django项目中添加这个模型文件和应用程序。

python manage.py startapp phpextension
mv models.py phpextension/models.py

Modify your installed apps in settings to include your newly installed app and you now have access to your whole PHP app data through your django app. Create your views for what data you wish expose on the web.

在设置中修改已安装的应用程序以包含新安装的应用程序,现在您可以通过django应用程序访问整个PHP应用程序数据。

As an added note, if you with to camouflage to the public that you are mixing php and django/python you can create your URL patterns with the following:

作为附加说明,如果您想要将php和django/python混合在一起,您可以使用以下方法创建URL模式:

url(r'^my_command.php$', 'phpextensions.views.my_command', name="my_command"),

For this to work I think you also need to set the setting APPEND_SLASH=False.

为此,我认为您还需要设置设置APPEND_SLASH=False。

The views will then see the django view as just another php script (only faster, more gracious and infused with magical unicorns ).

这些视图将看到django视图仅仅是另一个php脚本(只是速度更快,更亲切,并且充满了魔法独角兽)。

Access to your Django data through PHP

通过PHP访问Django数据

Here I would use a REST api. The simple way is just to build a JSON response in your view with json.dumps(my_dictionary_of_data_for_php) and the more enterprise way is to use tastypie.

这里我将使用REST api。简单的方法是在您的视图中使用JSON .dumps(my_dictionary_of_data_for_php)构建一个JSON响应,更企业级的方法是使用taste ypie。

Then you can access the data in PHP using

然后可以使用PHP访问数据

$fc = file_get_contents('http://yourdomain.com/path/to/django/my_command.php');
$django_data = json_decode(fc);