📜  使用Python的简单聊天室

📅  最后修改于: 2022-05-13 01:54:31.811000             🧑  作者: Mango

使用Python的简单聊天室

本文演示 - 如何设置一个简单的聊天室服务器并允许多个客户端使用客户端脚本连接到它。该代码使用套接字和线程的概念。

套接字编程

可以将套接字视为双向通信通道中的端点,并在服务器和一个或多个客户端之间建立通信。在这里,我们在每一端设置一个套接字,并允许客户端通过服务器与其他客户端交互。服务器端的套接字与服务器端的某个硬件端口相关联。任何具有与同一端口关联的套接字的客户端都可以与服务器套接字通信。

多线程

线程是单独运行任何其他线程的一组命令的子进程。因此,每次用户连接到服务器时,都会为该用户创建一个单独的线程,并且从服务器到客户端的通信沿着基于为每个客户端的身份而创建的套接字对象的各个线程进行。
我们需要两个脚本来建立这个聊天室。一个保持服务运行,另一个是每个客户端都应该运行以连接到服务器。

服务器端脚本

服务器端脚本将尝试建立一个套接字并将其绑定到用户指定的 IP 地址和端口(Windows 用户可能必须在其防火墙设置中对指定的端口号进行例外处理,或者可以使用已经打开了)。然后脚本将保持打开状态并接收连接请求,并将相应的套接字对象附加到列表中以跟踪活动连接。每次用户连接时,
将为该用户创建一个单独的线程。在每个线程中,服务器等待消息并将该消息发送给当前正在聊天的其他用户。如果服务器在尝试从特定线程接收消息时遇到错误,它将退出该线程。

用法

通过选择计算机上的任何一个作为服务器节点,并使用该计算机的私有 IP 地址作为服务器 IP 地址,可以在局域网上设置此服务器。
例如,如果局域网有一组分配的私有 IP 地址,范围从 192.168.1.2 到 192.168.1.100,那么这 99 个节点中的任何一台计算机都可以充当服务器,其余节点可以通过以下方式连接到服务器节点使用服务器的私有 IP 地址。必须小心选择当前未使用的端口。例如,端口 22 是 ssh 的默认端口,端口 80 是 HTTP 协议的默认端口。因此,最好不要使用或重新配置这两个端口以使其免费使用。
但是,如果服务器打算在本地网络之外访问,则需要公共 IP 地址才能使用。如果来自本地网络的节点(不是路由器的节点)希望托管服务器,这将需要端口转发。在这种情况下,我们需要将任何到达公共 IP 地址的请求重新路由到我们本地网络中的私有 IP 地址,因此需要端口转发。
有关端口转发的更多信息:链接
要运行脚本,只需从文章底部指定的 GitHub 链接下载它,然后将其保存在计算机上方便的位置。

/* Both the server and client script can then be run
   from the Command prompt (in Windows) or from bash 
   Terminal (Linux users) by simply typing 
   "python chat_server.py  " or "python client.py  ". 
   For example, */
python chat_server.py 192.168.55.13 8081
python client.py 192.168.55.13 8081

以下是必须始终运行以保持聊天室运行的服务器端脚本。

Python3
# Python program to implement server side of chat room.
import socket
import select
import sys
'''Replace "thread" with "_thread" for python 3'''
from thread import *
 
"""The first argument AF_INET is the address domain of the
socket. This is used when we have an Internet Domain with
any two hosts The second argument is the type of socket.
SOCK_STREAM means that data or characters are read in
a continuous flow."""
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
 
# checks whether sufficient arguments have been provided
if len(sys.argv) != 3:
    print ("Correct usage: script, IP address, port number")
    exit()
 
# takes the first argument from command prompt as IP address
IP_address = str(sys.argv[1])
 
# takes second argument from command prompt as port number
Port = int(sys.argv[2])
 
"""
binds the server to an entered IP address and at the
specified port number.
The client must be aware of these parameters
"""
server.bind((IP_address, Port))
 
"""
listens for 100 active connections. This number can be
increased as per convenience.
"""
server.listen(100)
 
list_of_clients = []
 
def clientthread(conn, addr):
 
    # sends a message to the client whose user object is conn
    conn.send("Welcome to this chatroom!")
 
    while True:
            try:
                message = conn.recv(2048)
                if message:
 
                    """prints the message and address of the
                    user who just sent the message on the server
                    terminal"""
                    print ("<" + addr[0] + "> " + message)
 
                    # Calls broadcast function to send message to all
                    message_to_send = "<" + addr[0] + "> " + message
                    broadcast(message_to_send, conn)
 
                else:
                    """message may have no content if the connection
                    is broken, in this case we remove the connection"""
                    remove(conn)
 
            except:
                continue
 
"""Using the below function, we broadcast the message to all
clients who's object is not the same as the one sending
the message """
def broadcast(message, connection):
    for clients in list_of_clients:
        if clients!=connection:
            try:
                clients.send(message)
            except:
                clients.close()
 
                # if the link is broken, we remove the client
                remove(clients)
 
"""The following function simply removes the object
from the list that was created at the beginning of
the program"""
def remove(connection):
    if connection in list_of_clients:
        list_of_clients.remove(connection)
 
while True:
 
    """Accepts a connection request and stores two parameters,
    conn which is a socket object for that user, and addr
    which contains the IP address of the client that just
    connected"""
    conn, addr = server.accept()
 
    """Maintains a list of clients for ease of broadcasting
    a message to all available people in the chatroom"""
    list_of_clients.append(conn)
 
    # prints the address of the user that just connected
    print (addr[0] + " connected")
 
    # creates and individual thread for every user
    # that connects
    start_new_thread(clientthread,(conn,addr))    
 
conn.close()
server.close()


Python3
# Python program to implement client side of chat room.
import socket
import select
import sys
 
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if len(sys.argv) != 3:
    print ("Correct usage: script, IP address, port number")
    exit()
IP_address = str(sys.argv[1])
Port = int(sys.argv[2])
server.connect((IP_address, Port))
 
while True:
 
    # maintains a list of possible input streams
    sockets_list = [sys.stdin, server]
 
    """ There are two possible input situations. Either the
    user wants to give manual input to send to other people,
    or the server is sending a message to be printed on the
    screen. Select returns from sockets_list, the stream that
    is reader for input. So for example, if the server wants
    to send a message, then the if condition will hold true
    below.If the user wants to send a message, the else
    condition will evaluate as true"""
    read_sockets,write_socket, error_socket = select.select(sockets_list,[],[])
 
    for socks in read_sockets:
        if socks == server:
            message = socks.recv(2048)
            print (message)
        else:
            message = sys.stdin.readline()
            server.send(message)
            sys.stdout.write("")
            sys.stdout.write(message)
            sys.stdout.flush()
server.close()


客户端脚本

客户端脚本将简单地尝试访问在指定 IP 地址和端口创建的服务器套接字。一旦连接,它将不断检查输入是来自服务器还是来自客户端,并相应地重定向输出。如果输入来自服务器,它会在终端上显示消息。如果输入来自用户,它将用户输入的消息发送到服务器,以便将其广播给其他用户。
这是客户端脚本,每个用户必须使用它才能连接到服务器。

Python3

# Python program to implement client side of chat room.
import socket
import select
import sys
 
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if len(sys.argv) != 3:
    print ("Correct usage: script, IP address, port number")
    exit()
IP_address = str(sys.argv[1])
Port = int(sys.argv[2])
server.connect((IP_address, Port))
 
while True:
 
    # maintains a list of possible input streams
    sockets_list = [sys.stdin, server]
 
    """ There are two possible input situations. Either the
    user wants to give manual input to send to other people,
    or the server is sending a message to be printed on the
    screen. Select returns from sockets_list, the stream that
    is reader for input. So for example, if the server wants
    to send a message, then the if condition will hold true
    below.If the user wants to send a message, the else
    condition will evaluate as true"""
    read_sockets,write_socket, error_socket = select.select(sockets_list,[],[])
 
    for socks in read_sockets:
        if socks == server:
            message = socks.recv(2048)
            print (message)
        else:
            message = sys.stdin.readline()
            server.send(message)
            sys.stdout.write("")
            sys.stdout.write(message)
            sys.stdout.flush()
server.close()

输出:

在下图中,终端左侧已初始化服务器,终端右侧已初始化客户端脚本。 (使用 tmux 完成终端拆分,'sudo apt-get install tmux')。出于初始化目的,您可以看到每当用户发送消息时,该消息以及 IP 地址都会显示在服务器端。

截图来自 2017-04-18 18-31-03

下图是两个人在同一台服务器上的基本对话。多个客户端可以以相同的方式连接到服务器!

截图来自 2017-04-18 18-31-03