How to pass parameters into php executed in python?

王林
Release: 2024-02-10 21:40:04
forward
416 people have browsed it

如何将参数传递到在 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))
Copy after login

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+"""'));
"""
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!