I need exchange data between python daemon (cluster nods send data to this daemon) and php script (apache) which, is accessed by webbrowsers. What do you recommend as technology which could establish some connection between them. Both, python daemon and apache/php is on the same machine.
Thank you.
What you are wanting to do is called IPC (inter process communication). Since these are common Linux applications I am going to refer you to this SO question which recommends Unix Domain Sockets.
For PHP look here and for Python look here.
If you want things to be synchronous use a named socket(Amazing feature on Unix systems.)
If you want things to be asynchronous use pickle(there is a php version of it too.)
Related
i need ideas to solve this:
I have a entire website in PHP (5.2) in a PHP "shared server", only i can use apache+PHP, CGI & NodeJS, no memcached, redis or another software.
And i need to comunicate the PHP and the NodeJS Script.
My first approach is using socket connection, creating in NodeJS a socket listener and connect to it witch PHP, and then, send commands, whait for response, and close connection (and end PHP Script). To the other side, i can call PHP script via ¿httprequest? ¿or using sockets again?
The problem of using sockets fron Node to PHP, i CANT leave PHP script runing with set_time_limit(0) because the fuc... server, need to "call" PHP for another way.
The NodeJS and Apache + PHP are in the same machine, i need to make the code for the fast response time (sockets better than web-calls).
Better ideas or other solutions?
thanks!
Try memcached it's easy and very fast.
Basically I want to use some kind of sockets (web sockets or node.js?) but I don't really know much about them and I want to send low latency data between html pages with javascript enabled on them.
I have php installed on the shared server and socket_connect() is a function included so i'm guessing php_sockets.dll is installed. I could potentially use this, but the php script is on a remote server so I don't know if i would be able to run it?
Should I use some other language that would be installed on a normal shared server?
EDIT: I don't have shell access..
http://www.flynsarmy.com/2012/02/php-websocket-chat-application-2-0/ this should give you a pretty good start in talking to php using html5's websocket api. You don't need Node.js to implement this.
I am using the following js plugin which allows me to use WebSockets on android and iOS with apps written in html5 (via phonegap in my case).
https://github.com/FreakDev/PhoneGap-Android-HTML5-WebSocket
What else do I need in order to use a websocket?
I have a basic server with bluehost that has PHP and MySQL installed.. What am I going to need to do?
First you can use a library like this one:
http://code.google.com/p/phpwebsocket/
Second, your host must let you create sockets. This mean PHP must have php_sockets.dll (Win) or sockets.so (Linux) extension enabled and a forwarded port from your server.
Also you need to run your php from commend line or somehow keep it alive for ever.
It is just like creating a normal socket in PHP.
*Edit:
WebSockets are just some sort of normal sockets. In websocket you can connect to a endpoint which is listening for connections and then communicate with it. Just like normal sockets but with simple differences in protocol and more limitations. For doing so you need a script or application to run for ever and handle all connections from webpages. But a php file will end just after request ended. For keeping a php file running for ever you need to run it from commend line which mean you need to have shell access, or you can use this code to run your php script for ever: (But you must think about a mechanism to call it only once)
ignore_user_abort(true);
ini_set('max_execution_time', 0);
set_time_limit(0);
You can test. If your application fail with error messages about not knowing a function like socket_connect or socket_bind then you don't have socket extension for php.
Here is phpwebsocket files for download:
http://phpwebsocket.googlecode.com/svn/trunk/%20phpwebsocket/
There is an example there too.
As "Tom van der Woerdt" said PHP is not designed for doing socket programming. Go for a non-scripting language and use a dedicated server or at least a vps for opening and managing sockets.
I'm trying to implement a socket server that will run in most shared PHP hosting.
The requirements are that the Socket server can be installed, started and stopped from PHP automatically without the user doing anything. It doesn't matter what language the socket server is written in, as long as it will run on the majority of shared hosting globally.
Currently, I've written a Socket Server with PHP that implements an Object Cache:
http://code.google.com/p/php-object-cache/
source: http://code.google.com/p/php-object-cache/source/browse/trunk/socket.class.php
However, PHP has to be compiled with sockets support, and not many servers run with PHP sockets support.
My real question is: What language should I implement the socket server in, and have maximum platform support and be invokable from within PHP.
In other words, what scripting language is the most common on PHP enabled Servers?
Or do I have to write the socket server in a compiled language to have it works across all servers?
Lets leave IIS out of the picture at the moment, just Linux servers. I don't think many PHP sites are running on IIS...
edit:
Sorry I think my question is not clear.
I'd like to know, what languages is best suited for creating a socket server given the following requirements:
The language must exist in shared hosting, alongside PHP running in Apache (not CLI).
The sockets support must be enabled natively, not via a required extension.
PHP must be able to write the deamon to file as well as start and stop the deamon.
I'm not asking for a solution for a single server. It has to run natively on the majority of shared hosting servers.
Any server can be stopped or started by PHP under Linux. Of course, if you are running a server which accepts sockets from the internet, then you can just connect directly to the server and tell it to shutdown. No need to go via PHP!
As for "starting a server from PHP", well, under Linux, anything can be started from pretty much anything. Just shell out to start the process and have it drop into daemon mode.
I'm a Perl fan myself. Not surprisingly, there's a
Perl Daemon library available.
If your hosting provider offers Perl script support, then you probably have permission to use "system" or backticks command. Then you can very likely start a daemon. However, you will need to use a non-privileged port (over 1024). Also, you should ASK THEM FIRST! They may not appreciate you tying up ports on their server. This is very definitely something you should discuss with your hosting provider.
It really depends on what the install requirements are. Often the easiest and most standard way to write a socket server is to write an inet.d service. This is a standard daemon on my unix machines, and it will fork a process and handle the socket level details. If you want your service to run on port below 1024 on Unix, this is one of the easier ways to get it done. However, the initial install requires root to configure inet.d.
If you shared hosting allows PHP to do an exec call, then you could start the daemon that way. Keep in mind though, it'll need to run above port 1024. You next need to decide if your program is going to be multi-threaded or multi-process. Typically Java programs are multi-threaded, while an Apache instance is normally multi-process.
Lastly, the host may have a firewall in place. This helps prevent shared hosting accounts from becoming part of a bot-net. If the firewall rules don't allow connections to other ports, you won't be able to connect to it remotely.
Does anyone know of a JS-based terminal client? Either something that initiates an SSH connection with a remote host or even something that simply communicates with a remote PHP script to execute commands and retrieve output. Does such a thing exist?
Check Anyterm, it uses a XmlHttpRequest channel to communicate with a daemon that uses a pseudo-terminal to communicate with a shell or other application.
http://www.masswerk.at/termlib/ looks interesting as a base.
http://sourceforge.net/projects/jsterminal
but you'd have to write your own commands to connect with the host
Shell in a box could be used also with a small Asus EEE-PC:
http://code.google.com/p/shellinabox/
It works great also for virtual machines in a cloud.
So far JSDom Shell is the only thing I have found, which seems relatively barebones but practical. If there are any other more substantial options, feel free to share!