Python中的客户端-服务器通信 [英] client-server communication in Python

查看:51
本文介绍了Python中的客户端-服务器通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了客户端与服务器之间的通信.

I have established a client-server communication.

问题是我不能发送多条消息,我试图修复它,但我不知道出了什么问题.

The problem is that I can't send more than one message and I tried to fix it and I don't know what's wrong.

这是我的代码:

**服务器代码和客户端代码在两个不同的python窗口中运行.

**The server code and client code are run in two different python windows.

服务器:

import socket

s = socket.socket()                      # Create a socket object
host = socket.gethostname()              # Get local machine name

port = 12345                             # Reserve a port for your service.
s = socket.socket()
s.bind((host, port))                     # Bind to the port

s.listen(5)                              # Now wait for client connection.

conn, addr = s.accept()
print('Got connection from ', addr[0], '(', addr[1], ')')

while True:
    data = conn.recv(1024)
    print(data.decode("utf-8"))
    if not data: 
        break
    conn.sendall(data)

conn.close() 

print('Thank you for connecting')

客户端:

import socket                           # Import socket module

host = socket.gethostname()          # Get local machine name
port = 12345                        # Reserve a port for your   service.
conn = socket.socket()                   # Create a socket object

conn.connect((host, port))

conn.sendall(b'Connected. Wait for data...') 

intosend = input("message to send:")
conn.sendall(bytes(intosend, 'utf-8')) 

data = conn.recv(1024)
intosend= "no"

while intosend != "quit":
    intosend = input("message to send:")
    conn.sendall(bytes(intosend, 'utf-8'))



conn.close()                                    # Close the socket when done


print(data.decode("utf-8"))

有人可以帮忙吗?

推荐答案

服务器端添加了一些保护.也许这就是您所需要的?客户端进程完成后,服务器将继续侦听连接.通过启动服务器,启动客户端,发送消息,再次启动客户端并发送另一条消息来尝试...

Server side with a little protection added. Perhaps this is what you need? The server will continue to listen for connections after the client process finishes. Try it by starting the server, starting the client, sending a message, starting the client again and sending another message...

import socket
import threading
import sys

s = socket.socket()                      # Create a socket object
host = socket.gethostname()              # Get local machine name

port = 12345                             # Reserve a port for your service.
s = socket.socket()
s.bind((host, port))                     # Bind to the port

s.listen(5)                              # Now wait for client connection.

def processMessages(conn, addr):
    while True:
        try:
            data = conn.recv(1024)
            if not data: 
                conn.close()
            print(data.decode("utf-8"))
            conn.sendall(bytes('Thank you for connecting', 'utf-8'))
        except:
            conn.close()
            print("Connection closed by", addr)
            # Quit the thread.
            sys.exit()


while True:
    # Wait for connections
    conn, addr = s.accept()
    print('Got connection from ', addr[0], '(', addr[1], ')')
    # Listen for messages on this connection
    listener = threading.Thread(target=processMessages, args=(conn, addr))
    listener.start()

这篇关于Python中的客户端-服务器通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆