jeudi 7 juin 2018

http.server if statement os.path.isfile() doesn't work

So this is my code:

from http.server import BaseHTTPRequestHandler, HTTPServer
import requests, json, os

PORT = 1337

class getHandler(BaseHTTPRequestHandler):
    def handleJSON(self, provider, data):
        if provider == "provider_1":
            json_data = json.loads(data)
            sl_token = json_data["access_token"]
            return sl_token
        elif provider == "provider_2":
            json_data = json.loads(data)
            pb_token = json_data["access_token"]
            return pb_token

    def do_GET(self):
        data = self.requestline

        self.send_response(200)
        self.send_header('Content-type','text/html')
        self.end_headers()
        self.wfile.write(b'You may close the tab now')

        print("Raw Data: " + data)

        if not os.path.isfile("PbToken.txt") and os.path.isfile("SlToken.txt"):
            if "GET /?code=" and "&state=" in data: # Provider_1
                print("Provider_1 Data:", data)
                pb_code = data[data.find("/?code=") + len("/?code="):data.find("&state=")]

            elif "GET /?code=" in data: # Provider_2
                print("Provider_2 Data:", data)
                sl_code = data.strip()
                sl_code = sl_code[sl_code.rindex("/?code=") + len("/?code="):sl_code.rindex(" ")]
        else:
            raise SystemExit

server = HTTPServer(('localhost', PORT), getHandler)
print("Started server on port", PORT)

server.serve_forever()

So from the class getHandler in the function do_GET(self) it never makes it past the if not os.path.isfile("PbToken.txt") and os.path.isfile("SlToken.txt"): statement (I've of course made sure the files aren't actually there). I want it to check if both of the files exist, if they don't do what's written below. If the files exist they should go straight to the else statement where it uses raise SystemExit. What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire