Why does PHP work with Ajax, but Python does not?

Building a PHP script to respond to an Ajax request is as simple as:

<?php $command = $_POST["command"]; if ($command == "say_hello") { $name = $_POST["name"]; echo json_encode(array("message" => "Hello, " . $name)); } ?> 

and at least if you are using jQuery on the client side, and if you specified a callback function in the original request, an array containing this message will be passed to this callback function.

But with Python it is not so simple. Or at least I did not understand how to make it so simple. If I just try to "print" the answer (in parallel with the PHP expression "echo" above), the client will not receive anything.

Whenever I looked on the Internet how to respond to an Ajax request in Python, the answer always involves using Django or another web infrastructure.

And I know these things are wonderful, but what does PHP do, what makes using such a package unnecessary? I would like to write my server side script in Python instead of PHP, but I would prefer a DIY approach to using a third-party structure for what should be (right?) A fairly simple task.

Any help? An example of how to do this would be greatly appreciated.

+4
source share
5 answers

But with Python it is not so simple. Or at least I did not understand how to make it so simple. If I just try to "print" the answer (in parallel with the PHP expression "echo" above), the client will not receive anything.

I may have some of the details wrong, but I will try to explain why this is so. Firstly, the PHP you are talking about is baked directly on the apache web server. When you perform echo , it outputs the result to the response stream (TCP connection between server and client). When you do print in python, what you do prints the result to standard (on the command line).

As most languages ​​work on the Internet, it is that the request arrives at some kind of web server, and this web server runs the program. The program is responsible for returning the stream that the web server takes and sends it to the client over a TCP connection.

I assume that the web server redirects PHP standards to the web server stream, and that is how PHP can use echo to return its result. This is an exception, not a rule.

So, in order to use python to serve requests, you need to have a web server that can somehow execute python. This is traditionally done using CGI. More recently, something like mod_python been used to host python in apache. Currently using wsgi or mod_wsgi , which is the standard defined for python-speaking web servers.

You might want to look at something like web.py , which is a minimalistic python web map. This will bring you closer to what you are trying to do.

+12
source

PHP was built with the Internet in mind from the start. Unlike Python, it was developed as a general-purpose language.

When you echo from PHP, you are actually writing a stream that is sent to the user as part of the HTTP response. When you print to python, the output is written to the stdout stream by default, which means that instead of sending to the user via http, the output is written to the console (or something that fixes stdout at the moment).

So, for PHP, HTTP is a first class citizen. For more general languages ​​like Python, Ruby, Erlang, C, C ++ and so many other languages. You have to communicate with HTTP in different ways. PHP already handles this connection through apache mod_php or through something like PHP-FPM.

Soooo ...

Regarding the creation of your own server side of the script, I would really like to object to it, since Python Frameworks replaces the layer on which PHP is built. Thus, creating a standard HTTP server as you see fit with Python will not be very simple. The reason this is so complicated is because you have to either interact with CGI or WSGI (the Python standard for working on the Internet), or create your own HTTP server. If you think you are doing the job, I highly recommend it! You probably would have learned a lot by doing this, but it will not be the easiest. The great thing is that so many libraries already care about this post for you. For example, if you are looking for something lightweight, I highly recommend using a microstructure like flask , which I personally use if I need something very simple. It is much easier to start than django, but it also has fewer batteries.

Hope this helps!

+4
source

There is a python module called json . Perhaps many answers relate to structures (e.g. Django), since json is web oriented and Django is tornado twisted, cherry, etc. - all these are the means by which python interacts with the network.

There are many different ways to do this, and they are covered in other issues. However, just to answer your question, python has a json library in its standard library .

+2
source

Most PHP installers are configured by default to serve php files in web directories by running a script. Most Python installers do not. The fastest way to serve Python using apache:

  • In your apache configuration file, add AddHandler cgi-script .py to which block can already serve PHP files.
  • open python scripts by adding #! / usr / local / bin / python to your system or any other path. python
  • Remember to print the headers first, then a new line, then the body.
  • use import cgi if you want to do something more complex than send the response body.

Please note that this is not the recommended way to launch a python website, just a quick, php-ish way.

0
source

Richards has the right decision.

In essence, python is a general-purpose scripting language. Thus, he needs a way to connect to the Internet, for example, by opening a port and programming a network, etc. You can also use something CGI to connect to Apache or another web server.

This is no different from how Java, Ruby, Perl or C / C ++ also get on the Internet. PHP actually does the same thing. It interacts with a web server and then CGI.

If you had to interpret PHP from the command line or compile, then you will have the same problems as in Python. The problem is not PHP vs Python, but how you use the scripting language of your choice, connected to the Internet using the HTTP protocol.

0
source

Source: https://habr.com/ru/post/1416265/


All Articles