Running Python within PHP

Beberapa framework menyediakan fasilitas server untuk bahasa Python misalnya Flask, Django, Pyramid, Bottle, dan CherryPy. Namun karena kebanyakan website telah menggunakan server berbasis PHP, misalnya Apache, Nginx, Microsoft IIS, Lighttpd, dan Caddy, beberapa pengembang memanfaatkan fasilitas PHP untuk menjalankan Python di server. Pada prinsipnya ada dua metode mengakses Python dalam PHP.

(Several frameworks provide server facilities for the Python language, such as Flask, Django, Pyramid, Bottle, and CherryPy. However, since most websites have been using PHP-based servers such as Apache, Nginx, Microsoft IIS, Lighttpd, and Caddy, some developers leverage PHP facilities to run Python on servers. In principle, there are two methods to access Python in PHP).

1. Kode Python dijalankan di web server (Python code is executed on the web server)

Kalau pada flask, style Python dijalankan dengan format HTML dengan %% (format jinja-2) PHP membutuhkan setting terlebih dahulu agar server mampu membaca format *.py agar dijalankan server. Agak sedikit rumit untuk Ubuntu seperti video berikut.

(In Flask, Python code is run using HTML format (with jinja-2), while in PHP, the server needs to be configured first to read the *.py format and execute it on the server. The configuration process may be a bit complicated for Ubuntu, as shown in the following video).

Di sini program *.py diintegrasikan denan html lewat mekanisme print seolah-olah mencetak string style HTML. Tentu saja header perlu ditambahkan.

(Here, the *.py program is integrated with HTML through the print mechanism, as if printing an HTML-style string. Of course, the header needs to be added).

Untuk yang versi Windows sedikit lebih mudah, dengan format header yang berbeda pula. Misalnya video singkat ini untuk XAMPP.

(For the Windows version, it is slightly easier with a different header format. For example, this short video for XAMPP).

Kelemahan metode ini adalah karena load ke web server maka membebankan web server.

(The drawback of this method is that it can burden the web server due to the load it creates).

2. Kode Python dijalankan oleh file PHP (Python code is executed by a PHP file)

Metode ini merupakan metode yang lazim dipakai karena file Python dijalankan di terminal server tanpa melibatkan secara realtime web server, dan jauh lebih cepat. Apalagi jika disertakan tambahan multitasiking dengan pooling processor.

(This method is commonly used because the Python file is run on the terminal server without involving the web server in real-time, making it much faster. Moreover, if multitasking with processor pooling is added, it can be even faster).

Dengan fungsi exec file Python dipanggil dengan input dengan library sys untuk mempermudah. Di sini seolah-olah file php dimanfaatkan untuk menjalankan file Python via terminal biasa, seperti menjalankan file Python dengan mengetik python <file *.py>. Kedua pernah saya gunakan. Jika ada metode lain silahkan beri masukan di komentar. Sekian, semoga bermanfaat.

(The Python file is called with the input using the sys library to make it easier using the exec function. Here, it is as if the PHP file is used to run the Python file via the regular terminal, like running a Python file by typing python <file *.py>. I have used both methods before. If there are other methods, please provide feedback in the comments. That’s all, hope it’s useful).

Iklan

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout /  Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout /  Ubah )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.