Is there a function to stream my PiCamera on Webserver - php

Is there a reasonable solution to stream a live Video from my PiCamera to an apache2 Server. I want that I can activate the stream using a button on the server and deactivate it the same way. However, I looked up a lot of solutions but didn't find one that belongs to my problem. Maybe someone here knows how to solve it.
On the apache2 server, I use Html, PHP and CSS. I use the PHP language to configure my other buttons with a python script.
Here you can see my PHP code that I use to activate a script:
<html>
<head>
<form method="post" >
<input type="submit" value="Schiessen" name="schiessen">
</form>
<title>MUW</title>
</head>
<body>
<?php
if(isset($_POST["schiessen"]))
{
$command = escapeshellcmd("/var/www/html/runMotors.py");
$output = shell_exec($command);
echo $output;
}
?>
</body>
Here is an example how I want that my server looks like. Most important is that in the middle is a sort of display which shows the live stream. It is very important that you can still use the buttons even if the camera is on. Therefore the camera display should be such as a rectangle in the middle.

You can use flask too. That is web framework for python.
for example from the flask home page that is given above:
from flask import Flask, escape, request
app = Flask(__name__)
#app.route('/')
def hello():
name = request.args.get("name", "World")
return f'Hello, {escape(name)}!'
#app.route('/run_motors')
def run_motors():
# method calls here.
return
app.run(host='localhost', port=3000)
Your python code will listen to the port that specified by you (5000 is the default one of flask) and when HTTP request comes to the specified route (in this situation the route is '/run_motors') it will make another method calls.

Related

How to compile and send result php code in python http server?

I am wriritng HTTP server with python socket.
I have a web page that contain PHP code like this
<!DOCTYPE html>
<html>
<body>
<?php
echo "My first PHP script!";
?>
</body>
</html>
which a want to send to client, when i send it normally the PHP file will not be compiled and only send like a string and nothing happen in browser.
if i want to cmpile and send php code what should i do?
i send file like this:
with open(filename[1:]) as f:
outputdata = f.read()
self.request.send(bytes('\nHTTP/1.1 200 OK\n\n','utf-8'))
for i in range(0, len(outputdata)):
self.request.send(bytes(outputdata[i],'utf-8'))
self.request.close()
Your script is working as "intended". Python will not magically parse and execute PHP code. You need a PHP interpreter for that.
You could of course write one in your python server if so inclined, but I would rather recommend you to take a look at either Python web development (if you want to roll your own server for educational purposes you may want to use an existing templating system like Jinja), or PHP development.

How to run a script using Django?

So I've conntected GMail API to my Django project. When I run quickstart.py alone in PyCharm it runs and works perfectly (that's the script that opens a new tab with GMail log in).
Great but now I have to give a user an opportunity to do the same. So I decided that I'll create a button and with pressing that button the quickstart.py will run and user will log in.
I tried that by creating an action.
Then I tried a usual 'a' tag.
And in both cases was error "Not found".
Also I even tried to run an php where I execute .py script.Sounds crazy.
<?php
echo exec('/quickstart.py');
?>
But the error is the same. I've also tried to play with url.py and write paths. I think I don't understand something. Please, explain.
So again and shortly: Press button -> Run quickstart.py
Seems like you have not to run quickstart.py, but create there some function, import your from quickstart import your_function into your views.py and call that your_function from your_custom_view.
Simplified logic like that:
from quickstart import your_function
def your_custom_view(request):
button_was_pressed = request.GET.get("button")
if button_was_pressed:
your_function()
return HttpResponse("Button pressed")
else:
return HttpResponse("No button pressed")
And make your Button work like a link (if you dont need a POST request), smth like:
Button
NOTE: This is not working code, but simplified logic, with so short information that you gave.
UPDATE 1:
settings.py:
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
views.py:
from your_project.settings import BASE_DIR
path_to_json = os.path.join(BASE_DIR, r'client_secret.json')
flow = client.flow_from_clientsecrets(path_to_json, SCOPES)

Is there a function in Django / Python similar to PHP flush() that lets me send part of the HTTP response to clients?

According to the performance tip from Yahoo:
When users request a page, it can take
anywhere from 200 to 500ms for the
backend server to stitch together the
HTML page. During this time, the
browser is idle as it waits for the
data to arrive. In PHP you have the
function flush(). It allows you to
send your partially ready HTML
response to the browser so that the
browser can start fetching components
while your backend is busy with the
rest of the HTML page.
Example:
... <!-- css, js -->
</head>
<?php flush(); ?>
<body>
... <!-- content -->
Is there a function in Django/Python that;s simialr to PHP's flush()?
Thanks
No. Is the short answer.
The long answer depends what you're using between the webserver and python:
You could implement it with WSGI but it wouldn't be a whimsical task.
Maybe start here?
http://www.python.org/dev/peps/pep-0333/#the-start-response-callable
http://www.evanfosmark.com/2008/06/simple-output-buffering-in-python/ - Good article on the topic. Should do exactly what you need using either option provided.
You can yield a partial response instead of returning it.

What is the quickest and easiest way to run a small php script on my html page?

What is the quickest and easiest way to run a small php script on my html page and what do I need to do to get it running? I'm asking because I use html and css all the time, but have never done anything in php. I'll be using it to create an email form that doesn't open an email client to send it.
First, your host needs to support PHP. Most do.
Make a basic script like this:
<?php
mail('your#email.com', 'Some Subject', print_r($_POST, true));
?>
Then, build yourself an HTML form that points to this script...
<form action="yourscript.php" method="post">
<input type="text" name="SomeField" />
<input type="submit" name="submit" value="Submit" />
</form>
That's all there is to it. HOWEVER, this is problematic. You will get spam. You need to implement CAPTCHA and such. Otherwise you will get e-mails all the time, even if someone just hits this script with their web browser and no POST data.
Read a tutorial and learn some PHP. It will help you in the long run.
There is also a great form example on tizag.com that will help you understand the components at work here. Basically, you have an HTML form with a few fields (SomeField, submit) and when someone submits this form it will send the data to yourscript.php via the POST method. The PHP script can then read the data in the $_POST array. PHP has a convenient mail() function that is great for sending basic e-mail messages. The print_r() function is used to show everything in an array, such as $_POST.
well, the form itself is html, your form will post ( or GET) to your php script and this will send the email and show output.
To execute the php script you need a webserver that supports php (IIS with the php module, apache with php module etc). Your webserver will host the script and then will execute it and return the output to the browser.
Also you need access to an SMTP server in order to send the email.
Look at php mail for basic usage, and mostly pear mail for a more complete solution ( including smtp auth).
You need to have php installed and configured properly with your server. Then it's as easy as this:
<?php
echo 'Hello World';
?>
Edit: Also, you may need to use the file extension .php on the page you are trying to run the script on. For example index.php - It may or may not work if the extension is .html
Does that work? If so you are ready to make your script. If not please provide more information about your hosting environment.
The .php file is translated just like a normal HTML page if you don't use the opening tag for PHP (), so you can just have normal HTML, and put the PHP somewhere on the page in tags.

Pass from PHP to Javascript

I have a little problem here, and no tutorials have been of help, since I couldn't find one that was directed at this specific problem.
I have 2 hosting accounts, one on a server that supports PHP. And the other on a different server that does not support PHP.
SERVER A = PHP Support, and
SERVER B = NO PHP Support.
On server a I have a php script that generates a random image. And On server b, i have a html file that includes a javascript that calls that php function on server a. But no matter how I do it, it never works.
I have the following code to retrieve the result from the php script:
<script language="javascript" src="http://www.mysite.com/folder/file.php"></script>
I know I'm probably missing something, but I've been looking for weeks! But haven't found any information that could explain how this is done. Please help!
Thank you :)
UPDATE
The PHP script is:
$theimgs= array ("images/logo.png", "images/logo.png", "images/logo.png", "images/logo.png", "images/logo.png");
function doitnow ( $imgs) {
$total = count($imgs);
$call = rand(0,$total-2);
return $imgs[$call];
}
echo '<img src="'.doitnow($theimgs).'" alt="something" />';
<img src="http://mysite.com/folder/file.php" alt="" /> ?
It's not clear, why you include a PHP file as JavaScript. But try following:
Modify your PHP Script so that it returns a image file directly. I'll call that script image.php. For further information, look for the PHP function: header('Content-type: image/jpeg')
In your JavaScript file use image.php as you would any normal image.
Include the JavaScript on server B as a *.js file.
UPDATE:
It's still not clear, why you need JavaScript.
Try as image.php:
$theimgs= array ("images/logo.png", "images/logo.png", "images/logo.png", "images/logo.png", "images/logo.png");
function doitnow ( $imgs) {
$total = count($imgs);
$call = rand(0,$total-2);
return $imgs[$call];
}
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'mypage.php';
header("Location: http://$host$uri/" . doitnow($theimgs));
And on server b:
<img src="www.example.org/image.php"/>
You didn't specify, but I assume the two servers have different domain/hostnames. You may be running into a browser security model problem (same origin policy).
If that's the case, you need to use JSONP.
You may be using outdated sources to learn, since the language attribute is deprecated and you should use type="text/javascript" instead. It's also not clear what kind of output does the .php script produce. If it's image data, why are you trying to load it as a script and not an image (i.e., with the <img> tag)?
Update: The script is returning HTML, which means it should be loaded using Ajax, but you can't do that if it's on a different domain due to the same origin policy. The reason nothing is working now is that scripts loaded using the <script> tag aren't interpreted as HTML. To pass data between servers, you should try JSONP instead.
It seems that server A generates an HTML link to a random image (not an image). The URL is relative to wherever you insert it:
<img src="images/logo.png" alt="something" />
That means that you have an images subdirectory everywhere you are using the picture. If not, please adjust the URL accordingly. Forget about JavaScript, PHP or AJAX: this is just good old HTML.
Update
The PHP Script displays pics randomly.
Pics are hosted on server A, and they
are indeed accessible and readable
from the internet. The PHP Script has
been tested by itself, and works.
If these statements are true, Māris Kiseļovs' answer should work. So either your description of the problem is inaccurate or you didn't understand the answer...

Categories