Python 使用IP和端口检查服务器是否正常运行

Python 使用IP和端口检查服务器是否正常运行,python,sockets,Python,Sockets,您好,我创建了我的服务器-客户机模型,工作正常。现在我正在尝试创建一个seprate GUI,它接受两个输入服务器ip和服务器端口,然后它检查服务器是否启动或关闭,但当我尝试将GUI与服务器连接时,它会抛出此错误 文件“/root/PycharmProjects/usb monitor/server/server.py”,第20行,在客户端threadconn.send('Hi'.encode())#send只接受字符串断开管道错误:[Errno 32]断开管道 但在gui上,它显示ip已启动,

您好,我创建了我的服务器-客户机模型,工作正常。现在我正在尝试创建一个seprate GUI,它接受两个输入
服务器ip
服务器端口
,然后它检查服务器是否启动或关闭,但当我尝试将GUI与服务器连接时,它会抛出此错误

文件“/root/PycharmProjects/usb monitor/server/server.py”,第20行,在客户端threadconn.send('Hi'.encode())#send只接受字符串断开管道错误:[Errno 32]断开管道

但在gui上,它显示
ip已启动
,但它在服务器端抛出上述错误。我相信这是我的
GUI
code中服务器的套接字连接问题

这是我的gui代码

import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton, QLabel, QLineEdit, QGridLayout, QMessageBox)
import socket
import time

retry = 1
delay = 4
timeout = 1
ip = '192.168.10.10'
port = 52000

class LoginForm(QWidget):

    def __init__(self):
        super().__init__()
        self.setWindowTitle('Login Form')
        self.resize(500, 120)

        layout = QGridLayout()

        label_IP = QLabel('<font size="4"> SERVER IP </font>')
        self.lineEdit_IP = QLineEdit()
        self.lineEdit_IP.setPlaceholderText('Please enter your server IP')
        layout.addWidget(label_IP, 0, 0)
        layout.addWidget(self.lineEdit_IP, 0, 1)

        label_PORT = QLabel('<font size="4"> PORT </font>')
        self.lineEdit_PORT = QLineEdit()
        self.lineEdit_PORT.setPlaceholderText('Please enter your server port')
        layout.addWidget(label_PORT, 1, 0)
        layout.addWidget(self.lineEdit_PORT, 1, 1)

        button_login = QPushButton('Login')
        button_login.clicked.connect(self.check_password)
        layout.addWidget(button_login, 2, 0, 1, 2)
        layout.setRowMinimumHeight(2, 75)

        self.setLayout(layout)

    def isOpen(self, ip, port ):
         s = socket.socket()
         s.settimeout(timeout)
         try:
            s.connect((ip, int(port)))
            data = s.recv(1024)
            if (data==b'Hi'):
             return True
         except:
            return False
         finally:
            s.close()

    def checkHost(self, ip, port):
        ipup = False
        for i in range(retry):
            if self.isOpen(ip, port):
                ipup = True
                break
            else:
                time.sleep(delay)
        return ipup

    def check_password(self):
        ip = self.lineEdit_IP.text()
        port = self.lineEdit_PORT.text()
        if self.checkHost(ip, port):
             print("ip is up")
        else:
             print("ip is not up")

if __name__ == '__main__':
    app = QApplication(sys.argv)
    form = LoginForm()
    form.show()
    sys.exit(app.exec_())
from socket import *
# Importing all from thread
import threading

# Defining server address and port
host = '192.168.10.10'
port = 52000
data = " "
# Creating socket object
sock = socket()
# Binding socket to a address. bind() takes tuple of host and port.
sock.bind((host, port))
# Listening at the address
sock.listen(5)  # 5 denotes the number of clients can queue

def clientthread(conn):
    # infinite loop so that function do not terminate and thread do not end.
    while True:
        # Sending message to connected client
        conn.send('Hi'.encode())  # send only takes string
        data =conn.recv(1024)
        print (data.decode())


while True:
    # Accepting incoming connections
    conn, addr = sock.accept()
    # Creating new thread. Calling clientthread function for this function and passing conn as argument.
    thread = threading.Thread(target=clientthread, args=(conn,))
    thread.start()

conn.close()
sock.close()