每当计算机连接到我的服务器时,是否可以ping计算机的IP地址以在GUI中联机(或脱机)显示它们?

时间:2019-05-04 12:29:38

标签: python-3.x sockets client-server chat

我有一个使用python开发的工作客户端和服务器聊天应用程序。我打算通过在单独的GUI中显示与服务器连接或断开连接的计算机来使其更加动态。进行的方式是什么?

这是一个简单的聊天应用程序。用户/客户端注册->登录->输入服务器pc的IP->弹出聊天寡妇->开始与连接到同一服务器的另一个用户聊天

尝试导入操作系统并ping通,但仅ping通计算机。我希望服务器在连接服务器后立即自动检测计算机。服务器是使用MySQL创建的。在Windows上运行需要pymysql。

服务器代码:

from threading import Thread
#import MySQLdb
import struct
import pickle
import pymysql


def accept_incoming_connections():
    """Sets up handling for incoming clients."""
    while True:
        global client
        client, client_address = SERVER.accept()
        print("%s:%s has connected." % client_address)
        addresses[client] = client_address
        Thread(target=choice).start()


def choice():
        a = client.recv(BUFSIZ).decode("utf8")
        if a =='1':
                Thread(target=reg_client, args=(client,)).start()
        else:
                Thread(target=login, args=(client,)).start()


def handle_client(client,name):  # Takes client socket as argument.
    """Handles a single client connection."""
    #name = client.recv(BUFSIZ).decode("utf8")
    welcome = 'Welcome %s! If you ever want to quit, type {quit} to exit.' % name
    client.send(bytes(welcome, "utf8"))
    msg = "%s has joined the chat!" % name
    broadcast(bytes(msg, "utf8"))
    clients[client] = name

    while True:
        msg = client.recv(BUFSIZ)
        if msg != bytes("{quit}", "utf8"):
            broadcast(msg, name+": ")
        else:
            client.send(bytes("{quit}", "utf8"))
            client.close()
            del clients[client]
            broadcast(bytes("%s has left the chat." % name, "utf8"))
            break


def reg_client(client):
    while True:
        a = client.recv(BUFSIZ)
        break;
    data_arr = pickle.loads(a)
    print(data_arr)
    #print(mobileno)
    #print(password)
    name=data_arr[0]
    mobileno=data_arr[1]
    password=data_arr[2]

    db = pymysql.connect("localhost","root","root","ChatApp" )
    cursor = db.cursor()
    sql="insert into user(name,mobileno,password) values(%s,%s,%s)"
    values=(name,mobileno,password)
    cursor.execute(sql,values)
    db.commit()
    cursor.close()
    db.close()
    Thread(target=choice).start()

def login(client):
    while True:
        a = client.recv(BUFSIZ)
        data_arr = pickle.loads(a)
        print(data_arr)
        break;
    mobileno=data_arr[0]
    password=data_arr[1]
    print (mobileno)
    print(password)
    db = pymysql.connect("localhost","root","root","ChatApp" )

    #sql='SELECT * FROM user1 WHERE mobileno = %s and password = %s',(mobileno,password)
    sql='SELECT * FROM user WHERE mobileno = %s and password = %s'
    values=(mobileno,password)
    cursor = db.cursor()
    cursor.execute(sql,values)
    data = cursor.fetchall()

    for row in data:
        global name
        name=row[0]
    print("Name=",name)

    if data != None:
        client.send(bytes('1', "utf8"))
        print("success")
        Thread(target=handle_client, args=(client,name)).start()
    else:
        client.send(bytes('0', "utf8"))
    cursor.close()
    db.close()
    Thread(target=choice).start()



def broadcast(msg, prefix=""):  # prefix is for name identification.
    """Broadcasts a message to all the clients."""

    for sock in clients:
        sock.send(bytes(prefix, "utf8")+msg)


clients = {}
addresses = {}

HOST = ''
PORT = 33000
BUFSIZ = 1024
ADDR = (HOST, PORT)

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

if __name__ == "__main__":
    SERVER.listen(5)
    print("Waiting for connection...")
    ACCEPT_THREAD = Thread(target=accept_incoming_connections)
    ACCEPT_THREAD.start()
    ACCEPT_THREAD.join()
SERVER.close()

0 个答案:

没有答案