[Python] How to get the public IP address of my Pythonanywhere server?

Discussão em 'Python' iniciado por Stack, Julho 18, 2025.

  1. Stack

    Stack Membro Participativo

    I'm making a multiplayer game in Python using sockets and I can't port-forward on my network, so I've decided to use Pythonanywhere for the server. However, I don't know the public IP address of my Pythonanywhere server. How do I get it so that the server and the client has it?

    Below is an example of my Python code with server-client basic communication.

    import socket
    import threading


    HEADER = 64 # Size of message that sets the size of the messages after if yk what I mean
    HOST = "0.0.0.0" # This is where I'm losing my mind
    PORT = 58879
    FORMAT = 'utf-8'
    DISCONNECT_MSG = "!DISCONNECT"

    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.bind((HOST, PORT))


    def handle_client(conn: socket.socket, addr):
    print(f"[NEW CONNECTION] {addr} connected.\n")

    connected = True
    while connected:
    data_len = conn.recv(HEADER).decode(FORMAT)
    data_len = int(data_len)
    data = conn.recv(data_len).decode(FORMAT)

    if data == DISCONNECT_MSG:
    connected = False

    print(f"[{addr}] {data}")
    conn.close()


    def start():
    server.listen()
    print(f"[LISTENING] Server is listening on {HOST}")
    while True:
    conn, addr = server.accept()
    thread = threading.Thread(target=handle_client, args=(conn, addr))
    thread.start()
    print(f"[ACTIVE CONNECTIONS] {threading.active_count() - 1}")


    print("[STARTING] server is starting...")
    start()

    Continue reading...

Compartilhe esta Página