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.
Related
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.
I send my data through get request from python to php website (it's on hosting) and it doesn't work. But when it's on localhost it works
Php:
<?php
$req_dump = print_r($_REQUEST, true);
$fp = file_put_contents('text.txt', $req_dump, FILE_APPEND);
?>
Python:
import requests
while True:
theWeight = input("Enter ")
r = requests.get('http://localhost:81/index.php', params={'weight': theWeight})
print(r.url)
I edit only this string for the site that's on hosting and it doesn't get the GET requests
r = requests.get('http://example.com/index.php', params={'weight': theWeight})
The problem is that your page requires JavaScript for work correctly as shown in the r.text output (i.imgur.com/k5hAEZ0.png).
Now your PHP code doesn't anything other than write the .txt file, there is some HTML/JS code under that?
If not it can be that your hosting provider web server has some filter for "no-js-enabled" HTTP requests? I find it strange that the code stops working only on the hosting server
Another way of solving this problem is by using another python lib that supports JavaScript: Web-scraping JavaScript page with Python
Well it was because of the hosting, I only changed it and it works
I tried the following coding for a 'UIWebView:
let kapitel3 = "<html><head><title>Chapter 1</title></head><body><h1>This is a title!</h1></body></html>"
This HTML code works fine and "UIWebView" shows this code. But if I try to insert a PHP code within the HTML code like this:
let kapitel3 = "<html><head><title>Chapter 1</title></head><body><h1>This is a title!</h1><?php print \"Hello world!\";?></body></html>"
Then the PHP code won't be showed. Why doesn't 'UIWebView' translate this PHP code <?php print \"Hello world!\";?>?
Is it not possible to integrate PHP code in a String like above?
Thanks for any hints!
UIWebView is only a simple web-browser...
You can't use php like that if you don't have a local webserver with a php interpreter installed!
You could load a php page from a remote url but your phone must have an active internet connection...
Possible local solution:
You can't use php locally, but in a UIWebView you can still use javascript!
I don't know what you are trying to do...but if you need your app to work offline (with local files) using php is the wrong approach.
I am building a search result page that needs to be formatted for the iPhone. I have built a browser based web application in Dashcode and input data from a php file. For this example I will call the php file test.php. Here is the basic model. (the i= is the query for the php)
web app sends i= --------> test.php --------> mysqldatabase
then
mysqldatabase ---------> test.php ----------> JSON output
then
JSON output ------> Dashcode Browser Graphical UI
The data is getting encoded, but not loading into Dashcode's browser UI. Ideas?
It's probably the same-origin policy. Under "Run & Share" enter the domain hosting your PHP file in the "Simulate running on domain:" field (and check the checkbox next to it).
If you want to go "cross-domain" while testing within dashcode… this proxy.php snippet allows you to enter a URL such as… http://myprivatedomain.com/proxy.php?url=https://some.twitter.jsonapi.url%8483948 and use it all, without whining, from DashCode….
<?php $filename = $_REQUEST['url'];
header('Content-Type: application/json');
ob_start();
json_encode(readfile($filename));
ob_flush(); ?>
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.