Home > Blockchain >  python unable to identify else statements
python unable to identify else statements

Time:01-10

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()

at line 32 vs code is giving an error message as expected expression pylance and unable to proceed further. please anyone help me to solve this error. i am getting two error one is in else and another is at return statement at line 43. so if anyone can able to identify the error and help me out to solve this please help me.

Error Line

CodePudding user response:

Indentation is significant in Python.

You have one line after your if indented, then lines which are not indented. This means the conditional is finished. You then have an else by itself, which is not permitted.

You likely meant:

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

But this would be improved by using a context manager:

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

CodePudding user response:

enter image description here

This is your code scope

Just indent 28-30 and 38-43 line , then part 1,2 will into if scope part 3,4 into func scope

  • Related