PHP calls Python script

Source: Internet
Author: User
Tags php script

Main reference two articles

A line break in PHP

Debugging Python with PHP

Python peek-the beginner to Python

These two articles combine to test, the main process is as follows

cd/var/www/htmlmkdir  Scriptnano script/test.py# paste code nano test002.php# paste code

The main problems encountered during the period are
PHP file inside the path/script/test.py, changed to script/test.py, the author obviously added directories in the root directory

PHP for the implementation of the line, want to achieve, originally with \ n, the result of failure, so with <br>

Continue to modify, found that the continuous iteration of the statement can not print the data, the specific reason is not queried, guessing is due to the Python itself error, you can run the script test locally.

Another test is the Chinese language support, found in Chinese when the page is blank, is obviously an error, it was expected that PHP is not good for Chinese support, so modified, added Chinese support, found that the situation is still, so the local test of Python Chinese support, found not supported, according to the error message

' \xe8 '  in file test.py on line 9, but no Encodi

Query the workaround. Originally need to start adding UTF8 support, add line comments done, the final PHP script content is

<?PHP#header ("content-type:text/html; Charset=utf-8 ");$output=shell_exec(' Python script/test.py ');$array=Explode(‘,‘,$output);foreach($array  as $value) {#echo "\ n";Echo $value;Echo"<br>";}?>

Python content is

#Coding=utf-8Print 'This was a Python program with PHP,'#print ' \ n 'Print ","Print ","Print ","Print ","Print ","Print 'Write A program This prints the numbers from 1 to 100. Multiples of three print "Fizz" instead of the number and for the multiples of five $Print 'Requirements: Printing 1 to 100, encountered a multiple of 3, only print "Fizz", encountered a multiple of 5, print "Buzz", while encountering multiples of 3,5, print "Fizzbuzz",' forXinchRange (1,101):Print"Fizz"[x%3*4:]+"Buzz"[X%5*4:]orxPrint "," forXinchRange (1,11):Print"Qiangge_is_god"[x%3*14:]+"I_can ' T_believe_it"[X%5*18:]orxPrint ","False=TrueifFalse:Print "Hello"Else:    Print " World"Print ','

The last displayed

PHP calls Python script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.