i'm creating embedded system with infinite Python script (launched on startup) and PHP web page. PHP web page have to communicate with that script.
Current solution is file based communication. PHP writes to file some command and Python writes response to some other (or same) file.
Another possible solution is to call some NOT infinite Python script only at need with PHP $response = shell_exec('./script.py');
Both solutions are possible but they are complicated and I need that Python script to be infinite.
Is there any way how to open some communication tunnel between two independents scipts on same linux device?
Like UART, telnet, etc. between two devices.
Now i'm searching for solution with PHP-PYTHON and PYTHON-PYTHON communication but sometimes i need this for example with Bash, TCL, etc.
Thank you (and sorry for not good english)
Radim
Without knowing much about what you are trying to achieve, i would go for a base http solution, starting a simple http server with python
import SimpleHTTPServer
import SocketServer
PORT = 8000
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "serving at port", PORT
httpd.serve_forever()
.... do something with the http request and send the response
Then the php script can simply make some GET/POST/etc.. request to python directly, and grab the http responses, for example via cURL:
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "localhost");
// set port
curl_setopt($ch, CURLOPT_PORT, 8000);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
Of course this is a simple stub and it should be tweaked a bit, but it should give you a starting point.
Related
My PHP server (A) needs to make an http request to another server (B) each time it's called.
I'm familiar with the usual use case for using curl in PHP
$ch = curl_init();
curl_setopt_array($ch, $curlOptions);
$responseBody = curl_exec($ch);
$curlErrorCode = curl_errno($ch);
curl_close($ch);
However I'm calling the same server (B) every time. Is there a way to keep the connections between A and B open (keepalive)? Maybe some sort of connection pool?
Implementing socket handling will be great if you want two servers to communicate with each other.
http://php.net/manual/en/sockets.examples.php
How can I print request I have sent, header and body? Following:
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
gives me just $r['request_headers']?
i made a server & website specifically for this purpose, http://dumpinput.ratma.net for the website, and https://github.com/divinity76/dumpinput.ratma.net for the server, note that it does not support httpS, so if the input is sensitive, you should probably run your own instance of the server (it's free & open source~), also note that i run the server on a dev-vm from cloudatcost.com, which has a bad reputation for uptime, so don't depend on the website being up.
you could also set up a netcat server as the target of the curl request (but that won't work with Expect 100 Continue POST-requests, but the dumpinput server will still work.)
I'm familiar with cURL for retrieving files, submitting POSTs, etc, but now I need a php script to "chat" with a networked hardware device. This device has a kind of Terminal Mode and is accessible through a shell with tools like netcat. I'm trying to reproduce that dialog with a php script.
So far I can open the connection and get the password request:
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_URL, 'http://192.168.0.35:4025');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
echo "DSC: $result \n";
// submit password
// read answer to see if password is approved
// submit command
// get command answer
curl_close($ch);
How can I now submit the password as a single string, over that existing cURL connection, and keep my script interacting back and forth sending and receiving strings?
Is there a better tool than cURL for this?
The plan is to keep the connection alive forever, for monitoring purposes.
Any assistance will be appreciated.
you can use socket check this
Socket : sockets are the fundamental "things" behind any kind of network communications done by your computer. For example when you type www.google.com in your web browser, it opens a socket and connects to google.com to fetch the page and show it to you. Same with any chat client like gtalk or skype. Any network communication goes through a socket.
I was using cURL to scrape content from a site and just recently my page stated hanging when it reached curl_exec($ch). After some tests I noticed that it could load any other page from my own domain but when attempting to load from anything external I'll get a connect() timeout! error.
Here's a simplified version of what I was using:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://www.google.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
$contents = curl_exec ($ch);
curl_close ($ch);
echo $contents;
?>
Here's some info I have about my host from my phpinfo():
PHP Version 5.3.1
cURL support enabled
cURL Information 7.19.7
Host i686-pc-linux-gnu
I don't have access to SSH or modifying the php.ini file (however I can read it). But is there a way to tell if something was recently set to block cURL access to external domains? Or is there something else I might have missed?
Thanks,
Dave
I'm not aware about any setting like that, it would not make much sense.
As you said you are on a remote webserver without console access I guess that your activity has been detected by the host or more likely it caused issues and so they firewalled you.
A silent iptables DROP would cause this.
When scraping google you need to use proxies for more than a few hand full of requests and you should never abuse your webservers primary IP if it's not your own. That's likely a breach of their TOS and could even result in legal action if they get banned from Google (which can happen).
Take a look at Google rank checker that's a PHP script that does exactly what you want using CURL and proper IP management.
I can't think of anything that's causing a timeout than a firewall on your side.
I'm not sure why you're getting a connect() timeout! error, but the following line:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
If it's not set to 1, it will not return any of the page's content back into your $contents.
Hello
I am working with a legacy system where an ASP.NET application posts an XML file to a server via curl.exe (this url to send is configurable by a .config file).
Now due to legacy system limitations, I need curl post this XML to my ubuntu server by changing the said .congfig file, modify the received XML as I need and finally curl post it to the real server.
How can this be done ? My guess is a php or a python script running under apache2 server, listening posts. Once received the xml file, do the required modifications on the file and post to the real curl server.
Via php or python, how can this be done ?
Since ASP.NET application is posting XML, you simply need to handle a normal POST request, modify XML to match your requirement and post it using cURL to the real cURL server. In PHP, it would look something like this (more or less meta code, error checking and additional logic is needed):
$xml = $_POST['xml'];
// do something with posted XML
.....
// post it to the "real" cURL server
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('xml' => $xml));
$result = curl_exec($ch);
curl_close($ch);
That's about it, check cURL documentation and use what is necessary for POST to work with your server, and your are all good.