簡體   English   中英

python 無法識別 else 語句

[英]python unable to identify else statements

from requests import *
import json
import base64
import urllib
from cmd import Cmd

url = "http://api.response.htb/"
url_digest = "cab532f75001ed2cc94ada92183d2160319a328e67001a9215956a5dbf10c545"


def get(url, url_digest): data = {
    "url": url,
    "url_digest": url_digest,
    "method": "GET",
    "session": "5f7bf45b02c832cf5b40c15ab6d365af",
    "session_digest": "a2b9ac69ab85795d13d12857a709a024cd729dcdf2c3fd3bb21ed514bc9990ac"
}


headers = {'Content-Type': 'application/json'}
url_proxy = "http://proxy.response.htb/fetch"
s = Session()
res = s.post(url_proxy, json=data, headers=headers)
body = json.loads(res.text)['body']
body = base64.b64decode(body)
if "zip" in url:
    f = open("file.zip", "wb")
f.write(body)
f.close()
print("Done saving file :-");

else: print body


def url_de(url):
    s = Session()


res = s.get('http://www.response.htb/status/main.js.php',
            cookies={'PHPSESSID': url})
x = res.text.find("session_digest':'")
y = res.text.find("'};")
return res.text[x+17:y]


class pr(Cmd):
    prompt = "==> "


def default(self, url): url_digest = url_de(url)


get(url, url_digest)
def do_exit(self, a): exit()


pr().cmdloop()

在第 32 行,vs 代碼按照預期的表達式 pylance 給出錯誤消息,無法繼續進行。 請任何人幫我解決這個錯誤。 我收到兩個錯誤,一個在 else 中,另一個在第 43 行的返回語句中。所以如果有人能夠識別錯誤並幫助我解決這個問題,請幫助我。

錯誤行

縮進在 Python 中很重要。

if縮進后有一行,然后是未縮進的行。 這意味着條件已完成。 然后你有一個else本身,這是不允許的。

你可能的意思是:

if "zip" in url:
    f = open("file.zip", "wb")
    f.write(body)
    f.close()
    print("Done saving file :-");
else: 
    print(body)

但這可以通過使用上下文管理器來改進:

if "zip" in url:
    with open("file.zip", "wb") as f:
        f.write(body)
    print("Done saving file :-");
else: 
    print(body)

在此處輸入圖像描述

這是您的代碼 scope

只需縮進 28-30 和 38-43 行,然后第 1,2 部分將進入 if scope 第 3,4 部分進入 func scope

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM