How can I test Node.js code to the browser? - php

I used to use PHP in my projects and MySQL of course, but I'm starting a course to Node.js right now and I really don't understand how it works. In PHP all I ever needed was an Apache as localhost and MySQL.
But when I use Node.js, what do I need ? The main question is how and where I can implement and write Node.js code and how I test it on the browser like I used to do in PHP?

Node.js is a runtime for JavaScript code that is specifically not run in the browser. To execute JavaScript in Node.js, you simply run Node.js from the command line and supply your JavaScript source file as an argument.
node myFile.js
This will execute whatever is in myFile.js. This file could look like the following:
let myVar = 1;
for (myVar; myVar < 5; myVar++) {
console.log(myVar);
}
This would print the following to the command line, when run:
1234

Related

Python Web Server with PHP code? [duplicate]

For some reason, I have to run a php script to get an image from Python. Because the php script is very big and it is not mine, it will takes me days to find out the right algorithm used and translate it into python.
I am wonder if there is any way to run php script, with few parameters, which returns a image, in python.
Example code:
import subprocess
# if the script don't need output.
subprocess.call("php /path/to/your/script.php")
# if you want output
proc = subprocess.Popen("php /path/to/your/script.php", shell=True, stdout=subprocess.PIPE)
script_response = proc.stdout.read()
You can simply execute the php executable from Python.
Edit: example for Python 3.5 and higher using subprocess.run:
import subprocess
result = subprocess.run(
['php', 'image.php'], # program and arguments
stdout=subprocess.PIPE, # capture stdout
check=True # raise exception if program fails
)
print(result.stdout) # result.stdout contains a byte-string
You can use php.py. This would allow you to execute php code in python, like in this example (taken from here):
php = PHP("require '../code/private/common.php';")
code = """for ($i = 1; $i <= 10; $i++) { echo "$i\n"; }"""
print php.get_raw(code)
If you can run the PHP script locally from the command-line, subprocess.check_output() will let you can PHP and will capture the return value.
If you are accessing PHP via a socket, then you can use urllib.urlopen() or urllib.urlretrieve() to pull down the resource.
Make a wrapper around the PHP script, which:
performs the stuff (If I understand well, it's an image creation),
then redirects (301 Moved Permanently) to the result image,
(also, the image should be purged some day).
So you can refer to this service (the PHP script) with a simple HTTP request, from anywhere, you can test it with browser, use from Python prg, you need just download the image the usual way.
Also, if you have a such standalone sub-system, don't feel bad about implement it with different language/technique. It has several advantages, e.g. you can install that service on a different host.
Recommended reading: Service-Oriented Architecture on Wikipedia.

Execute a php command from another php file

I having a simple problem, I guess.
I am working on an iPhone app which I can send ASIHTTPRequest to my php server (with go daddy). The php script then gets the command and run like:
$this->pdo->beginTransaction();
//do some other simple works
exec ('/usr/local/bin/php -f /path/to/my/script/test.php') ;
$this->pdo->commit();
which is suppose to run another php file within my own server (dedicated)!!! But it does NOT do anything. It does work with curl_exec() though, but I want to use another method which I can put it to work in the background server.
My planning was that I want to send too many APNS (notification) but instead of waiting for the whole list to be done, it is better to get back and let the work done in the background!! How can I do that.
When I got connected using SSH command line. I can easily call "test.php" and it works so fine. But I can not do the same thing from the above php code.
Any help is appreciated.
Use PHP Include? That will run the script.
Put something like this inside an IF Statement.
include 'YourPage.php';
My first thought is that GoDaddy might not allow exec() to be run for security purposes.

Execute php code in Python

For some reason, I have to run a php script to get an image from Python. Because the php script is very big and it is not mine, it will takes me days to find out the right algorithm used and translate it into python.
I am wonder if there is any way to run php script, with few parameters, which returns a image, in python.
Example code:
import subprocess
# if the script don't need output.
subprocess.call("php /path/to/your/script.php")
# if you want output
proc = subprocess.Popen("php /path/to/your/script.php", shell=True, stdout=subprocess.PIPE)
script_response = proc.stdout.read()
You can simply execute the php executable from Python.
Edit: example for Python 3.5 and higher using subprocess.run:
import subprocess
result = subprocess.run(
['php', 'image.php'], # program and arguments
stdout=subprocess.PIPE, # capture stdout
check=True # raise exception if program fails
)
print(result.stdout) # result.stdout contains a byte-string
You can use php.py. This would allow you to execute php code in python, like in this example (taken from here):
php = PHP("require '../code/private/common.php';")
code = """for ($i = 1; $i <= 10; $i++) { echo "$i\n"; }"""
print php.get_raw(code)
If you can run the PHP script locally from the command-line, subprocess.check_output() will let you can PHP and will capture the return value.
If you are accessing PHP via a socket, then you can use urllib.urlopen() or urllib.urlretrieve() to pull down the resource.
Make a wrapper around the PHP script, which:
performs the stuff (If I understand well, it's an image creation),
then redirects (301 Moved Permanently) to the result image,
(also, the image should be purged some day).
So you can refer to this service (the PHP script) with a simple HTTP request, from anywhere, you can test it with browser, use from Python prg, you need just download the image the usual way.
Also, if you have a such standalone sub-system, don't feel bad about implement it with different language/technique. It has several advantages, e.g. you can install that service on a different host.
Recommended reading: Service-Oriented Architecture on Wikipedia.

PHP in python through bash

As I am messing around with Python, I wanted to write an application that would have its own web server. I am currently using a written code from this website for http server: Python Web Server and this is the direct link of the script
This script can handle html but I wanted to add php to it too. Since I know that in Ubuntu I can run php commands within the terminal, I wanted to implement the same logic to this script.
I added these lines:
import os
os.system('php '+filepath)
However my plan didn't go as well as planned... The "<? echo 'hello world'; ?>" that was in my test.php echoed the string to the terminal that ran the webserver.py and it only returned the output message "0" to the web browser.
Is there a way to capture the output of the php file into a string in bash so that I can make the python webserver script output that string with os.system ?
I'm using Ubuntu 11.10.
You can use getoutput from the commands package for this simple case.
from commands import getoutput
response = getoutput('php myscript.php')
You should eschew the use of os.system() in favor of the more modern subprocess module.
You might especially look at popen.communicate().

How do I call a python scipt by php on a less powerful server to be run on an other more powerful server (cluster)?

I have a action.php script from where I call a python script like this:
$call_python = "../python/python myPythonScript.py ".$someArgument;
$python_output = Null; // stores every output generated by myPythonScript.py
$mystring = exec($call_python, $output_python);
I know, this is perhaps not the best way, but it works for me so far. Recently I discovered, that the computational power of the actual server is not sufficient. So I want the python script to be run on a more powerful server (a cluster) which is in the same network.
Is this possible?
You can easily replace your exec() call with ssh2_exec(). To get the output use stream_get_context() however. That's probably the sanest solution here.
You just need to open a connection to your secondary server beforehand. See ssh2_connect and ssh2_auth_password for that. And of course you need the ssh2 PHP extension installed and enabled.

Categories