请求库未发送请求

时间:2020-02-29 13:56:54

标签: python php

如果我手动发送第二个请求,它可以正常工作,但是如果我尝试通过python进行操作,则失败。 apache日志中没有错误。文件保持不变,不会被覆盖。 代码:

def fen_actions(mode,fen):
    global url

    if mode: # 1-> Read 2-> Write
        params = {'mode':'readFen'}
    else:
        params = {'mode':'putFen','fen':fen}

    r = requests.get(url,params)
    return r.text

fen_actions(2,chess.STARTING_BOARD_FEN) #This is not working. Starting fen is just a string
temp_readed_fen = fen_actions(1,0) # This works

php代码:

<?php


$f = fopen("tempfen.txt","r+") or die("Unable to open a file");

if (strval($_GET["mode"]) === strval("putFen"))
{
    if(strval($_GET['fen']) != strval(fread($f,filesize("tempfen.txt"))))
    {
        file_put_contents("tempfen.txt","");
        fwrite($f,$_GET['fen']);
    }

}

else if ($_GET["mode"] === strval("readFen"))
{
    echo(fread($f,filesize("tempfen.txt")));
}

ini_set('display_errors',1);
?>

1 个答案:

答案 0 :(得分:2)

如果我了解您要执行的操作,则认为您的if语句实际上并没有检查您想要的内容。根据您的评论,应该是:

if mode == 1: # 1-> Read 2-> Write
    params = {'mode':'readFen'}
elif mode == 2: 
    params = {'mode':'putFen','fen':fen}