Skip to content

How to pass the input_data into stdin in python? #61

@CannotBeFatAnyMore

Description

@CannotBeFatAnyMore

I call this API with a JSON file, but i find that the input_data is not received correctly by the python script, how to handle it? The test script is put below:

import json
import requests
def run_code_with_input(code_file_name, input_data):
    try:
        with open(code_file_name, 'r', encoding='utf-8') as f:
            code_content = f.read()
            ''' code file content:
            import sys

            print("开始读取输入...")
            line = sys.stdin.readline().rstrip()
            print(f"读到了: {line}")

            '''
        payload = {
            "compile_timeout": 10,
            "run_timeout": 10,
            "code": code_content,
            "input": input_data,
            "language": "python"
        }
        response = requests.request("POST", URL, data=json.dumps(payload))

        # response = requests.post(URL, data=json.dumps(payload), timeout=60)
        result = response.json()["run_result"]
        print(result)
        # 获取标准输出和错误输出
        output = result["stdout"]
        errors = result["stderr"]
        return output, errors
    except requests.exceptions.RequestException as e:
        print(f"请求异常: {e}")
        exit(1)

run_code_with_input("test_input.py", "Hello World\n")

The output result is:

{'status': 'Finished', 'execution_time': 0.01669454574584961, 'return_code': 0, 'stdout': '开始读取输入...\n读到了: \n', 'stderr': ''}
输出: 开始读取输入...
读到了: 

错误:

It should print "Hello World"....

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions