Home  >  Article  >  Backend Development  >  How to pass parameters into php executed in python?

How to pass parameters into php executed in python?

王林
王林forward
2024-02-10 21:40:04417browse

如何将参数传递到在 python 中执行的 php 中?

Question content

I need to pass a parameter into php code executed in python

import subprocess
def php(code):
    p = subprocess.Popen(["php","-r", code],
                         stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out = p.communicate() #returns a tuple (stdoutdata, stderrdata)
    if out[1] != b'': raise Exception(out[1].decode('UTF-8'))
    return out[0].decode('UTF-8')
val = str(1234)
code = """ \
function my_encoder($in){
    $out = base_convert($in,16,36);
return ($out);}

print(my_encoder('"""+{val}+"""'));
"""
print(php(code))

I don't know how to enter val as parameter of my_encoder function in php code.


Correct answer


If {, } is not part of python's f-string, do not use it because This results in invalid syntax. So just remove these curly braces:

print(my_encoder('"""+val+"""'));
"""

The above is the detailed content of How to pass parameters into php executed in python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete