簡體   English   中英

Python 套接字編程:從服務器到同一個客戶端的多個響應

[英]Python Socket Programming: multiple responses from server to the same client

郵件服務器.py

import socket
import sys
import os 
import threading

def respond (cmdList, user): #handling server response to the client
    global userList
    global passwordList
    
    cmd = cmdList[0] #extract relative command
    if cmd == "#EXIT":
        response = "250 Exit ok"
    if cmd == "#USERNAME":
        if (cmdList[1] + "\n") in userList:
            response = "250 Username ok"
        else:
            response = "200 Username does not exist"
            print (userList)
        
    
    return response

def thd_func(client):
    connectionSocket, addr = client
    command = connectionSocket.recv(1024)
    cmdList = command.decode().split(" ", 1)
         
    response = respond(cmdList, "")
    connectionSocket.send(response.encode())
    connectionSocket.close()

def main(argv):

    #initialize
    global userList
    global passwordList
    userList = []
    passwordList = []
    
    #create and bind the socket
    serverPort = int (argv[1])
    serverSocket = socket.socket()
    serverSocket.bind(("", serverPort))
    
    #fetch client information
    file = open ("ClientInfo.txt", "r")
    temp = file.readlines()
    file.close()
    for i in range (len(temp)):
        line = temp[i]
        if i % 2 == 0:
            userList.append(line)
        else:
            passwordList.append(line)
    
    #listen for incoming request
    serverSocket.listen(5)
    
    while True:
        client = serverSocket.accept()
        newthd = threading.Thread (target = thd_func, args = (client,))
        newthd.start()
    
    #close the socket
    serverSocket.close()

if __name__ == '__main__':
    
    #validation
    if len(sys.argv) != 2:
        print("Error: illegal parameter input") #port
        sys.exit(1)
        
    main(sys.argv)

郵件客戶端.py

import socket
import sys

def main(argv):   
    #create and connect the socket
    clientSocket = socket.socket()
    serverName = argv[1]
    serverPort = int (argv[2])
    clientSocket.connect((serverName, serverPort))
    
    while True:
        command = input ("Client : ")
        clientSocket.send(command.encode())
        response = clientSocket.recv(1024)
        print("Server : " + response.decode())
        if response.decode() == "250 Exit ok":
            break
        
    #close the socket
    clientSocket.close()

if __name__ == '__main__':
    if len(sys.argv) != 3:
        print("Error: illegal parameter input") #address port
        sys.exit(1)
    main(sys.argv)

我是Socket編程的初學者。 我正在做一個項目,該項目需要制作一個簡單的郵件服務器,可以讓用戶通過輸入簡單的命令登錄並執行一些簡單的任務。 因此,為了做到這一點,服務器需要多次處理來自同一個客戶端的輸入命令並做出相應的響應。 MailServer.py中,我正在嘗試使用多線程方法來做到這一點。 但是,它仍然不起作用,並且 cmd 仍然響應“ConnectionAbortedError:[WinError 10053] 已建立的連接被主機中的軟件中止”。 有什么我想念的嗎? 我該如何解決? 謝謝你。

def thd_func(client):
    while True:
        connectionSocket, addr = client
        command = connectionSocket.recv(1024)
        cmdList = command.decode().split(" ", 1)
             
        response = respond(cmdList, "")
        connectionSocket.send(response.encode())
        if response == "250 Exit ok":
            connectionSocket.close()

所以我修改了MailServer.py中的response function並修復了錯誤。 如果您仍然看到任何錯誤,請隨時為此問題添加更多評論或答案。 謝謝你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM