套接字服务器 IPv6 替代方案

时间:2021-04-21 11:55:39

标签: python

我正在编写一个套接字程序,我想与其他机器连接,但我需要一个 IPv6 来做到这一点。 有人知道替代方案吗?

我需要这个来做一个基本的私人消息服务器,所以我必须将 2 台机器(pc)连接到服务器,没有 IPv6 我只能连接到我的 IPv4,所以我只能创建一个本地服务器。

如果您想检查,这里是脚本:

import threading
import time

HEADER = 64
PORT = 8080
SERVER = "IPV6 ALTERNATIVES"
ADDR = (SERVER, PORT)

FORMAT = "utf-8"
DISCONNECT = "!DISCONNECT"
DISCONECT_MSG = "Can't start the server"

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)

users = []


def on_message(conn, msg):
    global users

    if not conn in users:
        users.append(conn)

    for user in users:
        user.send(f"{msg}".encode(FORMAT))



def handle_client(conn, addr):
    print(f"[{time.ctime(time.time())}] [NUEVA CONEXION] {addr} conectada.")

    connected = True
    while connected:
        msg_leigth = conn.recv(HEADER).decode(FORMAT)
        if msg_leigth:
            msg_leigth = int(msg_leigth)
            msg = conn.recv(msg_leigth).decode(FORMAT)
            if msg == DISCONNECT:
                connected = False  
            print(f"[{addr}] {msg}")
            #conn.send("Msg recibido".encode(FORMAT))
            on_message(conn, msg)          
    conn.close()


def start():
    server.listen()
    print(F"[{time.ctime(time.time())}] [SERVER] Hosteado en {SERVER}")
    while True:
        conn, addr = server.accept()
        thread = threading.Thread(target=handle_client, args=(conn, addr))
        thread.start()
        print(f"[CONEXIONES] {threading.activeCount() -1}")
        if False:
            print(DISCONECT_MSG)
    



print(f"[{time.ctime(time.time())}] [SERVER] Starting server...")
start()

0 个答案:

没有答案