import os
import socket
import threading
import tkinter as tk
from tkinter import filedialog, messagebox
from http.server import HTTPServer, SimpleHTTPRequestHandler
from base64 import b64encode, b64decode
import requests

class AuthHTTPRequestHandler(SimpleHTTPRequestHandler):
    def init(self, *args, password=None, **kwargs):
        self.password = password
        self.authenticated = False
        super().init(args, **kwargs)

    def do_GET(self):
        if not self.authenticated:
            self.authenticate()
        if self.authenticated:
            super().do_GET()
    
    def authenticate(self):
        auth_header = self.headers.get("Authorization")
        if auth_header:
            auth_type, encoded_credentials = auth_header.split(" ", 1)
            if auth_type.lower() == "basic":
                credentials = b64decode(encoded_credentials).decode()
                username, password = credentials.split(":", 1)
                if password == self.password:
                    self.authenticated = True
        
        if not self.authenticated:
            self.send_response(401)
            self.send_header("WWW-Authenticate", "Basic realm=“Authentication required")
            self.end_headers()

class FileServerApp:
    def init(self, root):
        self.root = root
        self.root.title("Local File Sharing Web Server")
        
        self.port = tk.IntVar(value=8000)
        self.directory = tk.StringVar()
        self.password = tk.StringVar()
        self.server = None
        self.thread = None

        self.use_ddns = tk.BooleanVar(value=False)
        self.ddns_provider = tk.StringVar()
        self.ddns_domain = tk.StringVar()
        self.ddns_token = tk.StringVar()

        self.ip_address = tk.StringVar()

        self.create_widgets()

    def create_widgets(self):
        tk.Label(self.root, text="Port:").grid(row=0, column=0, padx=5, pady=5, sticky=tk.E)
        tk.Entry(self.root, textvariable=self.port, width=10).grid(row=0, column=1, padx=5, pady=5)

        tk.Label(self.root, text="Shared Folder:").grid(row=1, column=0, padx=5, pady=5, sticky=tk.E)
        tk.Entry(self.root, textvariable=self.directory, width=40).grid(row=1, column=1, padx=5, pady=5)
        tk.Button(self.root, text="Browse", command=self.browse_directory).grid(row=1, column=2, padx=5, pady=5)

        tk.Label(self.root, text="Password:").grid(row=2, column=0, padx=5, pady=5, sticky=tk.E)
        tk.Entry(self.root, textvariable=self.password, show="", width=40).grid(row=2, column=1, padx=5, pady=5)

        self.start_button = tk.Button(self.root, text="Start Server", command=self.start_server)
        self.start_button.grid(row=3, column=0, columnspan=3, padx=5, pady=5)

        self.stop_button = tk.Button(self.root, text="Stop Server", command=self.stop_server, state=tk.DISABLED)
        self.stop_button.grid(row=4, column=0, columnspan=3, padx=5, pady=5)

        self.status_label = tk.Label(self.root, text="Server stopped.", fg="red")
        self.status_label.grid(row=5, column=0, columnspan=3, padx=5, pady=5)

        # New IP Address search button and display
        tk.Button(self.root, text="Search IP", command=self.search_ip).grid(row=6, column=0, padx=5, pady=5)
        tk.Label(self.root, text="IPv4 Address:").grid(row=6, column=1, padx=5, pady=5, sticky=tk.E)
        tk.Entry(self.root, textvariable=self.ip_address, state="readonly", width=15).grid(row=6, column=2, padx=5, pady=5)

        tk.Checkbutton(self.root, text="Use Dynamic DNS", variable=self.use_ddns, command=self.toggle_ddns_fields).grid(row=7, column=0, columnspan=3, padx=5, pady=5)
        
        self.ddns_frame = tk.Frame(self.root)
        self.ddns_frame.grid(row=8, column=0, columnspan=3, padx=5, pady=5)
        
        tk.Label(self.ddns_frame, text="DDNS Provider:").grid(row=0, column=0, padx=5, pady=5, sticky=tk.E)
        tk.Entry(self.ddns_frame, textvariable=self.ddns_provider, width=20).grid(row=0, column=1, padx=5, pady=5)
        
        tk.Label(self.ddns_frame, text="DDNS Domain:").grid(row=1, column=0, padx=5, pady=5, sticky=tk.E)
        tk.Entry(self.ddns_frame, textvariable=self.ddns_domain, width=20).grid(row=1, column=1, padx=5, pady=5)
        
        tk.Label(self.ddns_frame, text="DDNS Token:").grid(row=2, column=0, padx=5, pady=5, sticky=tk.E)
        tk.Entry(self.ddns_frame, textvariable=self.ddns_token, show="", width=20).grid(row=2, column=1, padx=5, pady=5)
        
        self.toggle_ddns_fields()

    def toggle_ddns_fields(self):
        if self.use_ddns.get():
            self.ddns_frame.grid()
        else:
            self.ddns_frame.grid_remove()

    def browse_directory(self):
        directory = filedialog.askdirectory()
        if directory:
            self.directory.set(directory)

    def start_server(self):
        if not self.directory.get() or not self.password.get():
            messagebox.showwarning("Warning", "Please select a directory to share and set a password.")
            return

        port = self.port.get()
        directory = self.directory.get()
        password = self.password.get()

        def run_server():
            os.chdir(directory)
            handler = lambda *args, **kwargs: AuthHTTPRequestHandler(*args, password=password, **kwargs)
            self.server = HTTPServer(("0.0.0.0", port), handler)
            public_ip = self.get_public_ip()
            local_ip = self.get_local_ip()
            self.update_status(f"Serving HTTP on port {port}\nLocal: http://{local_ip}:{port}/\nPublic: http://{public_ip}:{port}/", "green")
            self.server.serve_forever()

        self.thread = threading.Thread(target=run_server, daemon=True)
        self.thread.start()
        self.start_button.config(state=tk.DISABLED)
        self.stop_button.config(state=tk.NORMAL)

    def search_ip(self):
        ip = self.get_ip_address()
        self.ip_address.set(ip)

    def get_ip_address(self):
        try:
            # This method gets the IP address of the first non-loopback interface
            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            s.connect(("8.8.8.8", 80))
            ip = s.getsockname()[0]
            s.close()
            return ip
        except Exception:
            return "Unable to get IP"

    def get_public_ip(self):
        try:
            response = requests.get("https://api.ipify.org", timeout=5)
            response.raise_for_status()  # Raises an HTTPError for bad responses
            return response.text
        except requests.RequestException as e:
            return f"Unable to get public IP: {str(e)}"

    def get_local_ip(self):
        return socket.gethostbyname(socket.gethostname())

    def stop_server(self):
        if self.server:
            self.server.shutdown()
            self.server.server_close()
            self.server = None
            self.update_status("Server stopped.", "red")
            self.start_button.config(state=tk.NORMAL)
            self.stop_button.config(state=tk.DISABLED)

    def update_status(self, message, color):
        self.status_label.config(text=message, fg=color)

def main():
    root = tk.Tk()
    app = FileServerApp(root)
    root.mainloop()

if name == "main":
    main()


error:

Traceback (most recent call last):
  File "C:\Users\yaniv\Desktop\Remote\test.py", line 175, in <module>
    if name == "main":
       ^^^^
NameError: name 'name' is not defined



Note: no need to write the full code code only tell me what to change