简体   繁体   中英

php exec() of a python script

i made this small php code to execute a python script i'll use but it returns an empty array. Python and php files are in the same directory and i am using VISTA. Python script is supposed to receive a string representing the information in a file, so in the exec() i simulated that. In the commmand line it works fine and prints in this example:

content-type: text/html

Smiles

php file

$out=''
exec("python convert_str.py Content-Type: text/html,,('ref', {'Asay_parameter': ['grfbg'], 'Smiles': ['535'], 'Cell_type': ['4t4t4'], 'Subcellular': ['4trgdf'], 'CAS': ['12233'], 'target_type': ['derj,.i'], 'LAB': ['4t4t'], 'Species': ['4t34t4'], 'Bio_target': ['vgery4'], 'strain': ['wde'], 'Unity': ['htht5u'], 'Tissue': ['kukl'], 'value': ['gjyy'], 'Observations': ['gergh'], 'Experimental_error': ['tht56'], 'InchI': ['2435d'], 'Comparisons': ['thyt5'], 'Assay_ID': ['hj6yu'], 'Conditions': ['345y4'], 'Institution': ['4t'], 'Mol_name': ['3r5rg']})",$out) ;
print_r($out);

python file:

#!C:\Python27\python.exe


print "Content-Type: text/html\n"

import sys
import ast
superstring= sys.argv[1:]


def join_strings(megastring):

    total=''
    for substring in superstring:        
        total+=substring
    return total


def convert(string):

    save=''
    count=0
    for c in string:

        if c=='{' :
            ini=count
        elif c=='}':
            end=count+1
        count+=1

    save+=string[ini:end]
    final = ast.literal_eval(save)

    return final

def mol_id_type(info_dict):

    if info_dict['Smiles']!='':
        return 'Smiles'
    elif info_dict['InchI']!='':
        return 'InchI'
    else:
        return 'CAS'

def mol_id(info_dict,tp):
    """returns the id itself"""
    return info_dict[tp]




large=join_strings(superstring)
dic=convert(large)
mol_type=mol_id_type(dic)
identification=mol_id(dic,mol_type)

print mol_type

Output of php:

array()

I am pretty sure that the method by which you feed your Python script with input data is flaky. Do not rely on the many levels of command line argument parsing/encoding/decoding. It is entirely possible that this is where the difference comes from that you observed, between command line (your shell, I guess) and calling Python from PHP via exec() .

Better pipe this data into your Python process (ie write it to stdin of the Python process, and read it from there), or write it to a file from within PHP and then read that file from Python. Doing so, so can be sure that nothing messes with the byte stream which is your input data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM