1

so I have a python webserver and I can access the site when I connect to the localhost on my pc. I tried to connect to the server via my ipv4 address and followed the top awnsers here How can I access my localhost from my Android device?. However not one awnser helped me and I cant connect from my mobile phone to my ipv4 address as well as from my pc to the ipv4 address, localhost works. What I already did:

  • went to wifi settings and set wifi to private
  • allowed python in windows firewall
  • created a new rule in windows system panel and opened the port (8000)

Not any of those helped me to connect. Does someone know how I can fix this? Here is the code of my python server:

from http.server import BaseHTTPRequestHandler, HTTPServer
from datetime import datetime

hostName = "localhost"
serverPort = 8000
file_name = "C://Users//einfa//Desktop//server//chat.html"
all_messages = []

class MyServer(BaseHTTPRequestHandler):
    def get_message(self):
        message = ''
        start = False
        for symbol in self.path:
            if start:
                message += symbol

            if symbol == '=':
                start = True
        
        fixed_message = ''
        for symbol in message:
            if symbol == "+":
                fixed_message += ' '
            else:
                fixed_message += symbol
        return fixed_message

    def do_GET(self):
        file_text = ""
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()
        file = open(file_name, "r")
        
        now = datetime.now()
        current_time = now.strftime("%H:%M")

        if not self.get_message() in ["", " "]:
            all_messages.append("<li><div id=\"down\" class=\"message\">"+self.get_message()+"</div></li><strong>"+str(current_time)+"</strong>\n")

        for a in range(len(all_messages)-1):
            all_messages[a] = all_messages[a].replace("id=\"down\"", " ")

        for i in range(61):
            file_text += str(file.readline())
        
        for element in all_messages:
            file_text += element

        for i in range(1000):
            file_text += str(file.readline())
        file.close()

        self.wfile.write(bytes(file_text, "utf-8"))

if __name__ == "__main__":        
    webServer = HTTPServer((hostName, serverPort), MyServer)
    print("Server started http://%s:%s" % (hostName, serverPort))

    try:
        webServer.serve_forever()
    except KeyboardInterrupt:
        pass

    webServer.server_close()
    print("Server stopped.")

Thanks for reading

  • As the name "localhost" suggests, it's local. If you want to access it on another device in the same network, you typically need to bind your server to the device's LAN address between `192.168.1.0` and `192.168.1.127`. – Edward Ji Jun 02 '22 at 15:47
  • 1
    THANK U, it worked, I am so bad – HelpersCoockies Jun 02 '22 at 15:55

0 Answers0