简体   繁体   English

套接字编程客户端冻结python

[英]socket programming client freezing python

Basically, I have a python client to send command to ask server do somethings, I have implemented threading, but it seems not working.基本上,我有一个 python 客户端发送命令要求服务器做一些事情,我已经实现了线程,但它似乎不起作用。 For example, when I send "start" command to the server, it will execute job_function, but during this time, the client is freezing.例如,当我向服务器发送“启动”命令时,它会执行job_function,但在此期间,客户端冻结。 So basically, I want my server still able to respond to client's request, while executing tasks, and that's the reason I use thread.所以基本上,我希望我的服务器在执行任务时仍然能够响应客户端的请求,这就是我使用线程的原因。

Server.py:服务器.py:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('127.0.0.1', 5005))  # Bind to the port
s.listen(1)  # Now wait for client connection.
c, addr = s.accept()  # Establish connection with client.

while True:
    print('Got connection from', addr)
    data = c.recv(1024).decode()
    print(data)
    if data == "start":
        print("start the service")
        c.sendall('started service'.encode())

        threading.Thread(target=job_function).start()

    elif data == "suspend":
        print("suspend service")
        c.sendall('suspended service'.encode())
    else:
        c.sendall('wrong command'.encode())

c.close()  # Close the connection

Client.py:客户端.py:

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1',5005))

while True:
   x = input("Enter command: ")
   s.sendall(x.encode())
   print(x)
   data = s.recv(1024).decode()
   print(data)
s.close()

It can also be done only using recv() function.也可以仅使用 recv() 函数来完成。

client side:客户端:

                    sssize = os.stat('fileName').st_size
                    file = open('fileName', 'rb')
                    amm = file.read(sssize)

                    dfd = amm
                    #########
                    c.send(bytes(str(sssize), 'utf-8'))
                    rrespo = c.recv(1024).decode()

                    ######
                    if rrespo == 'yess':
                        m = 0
                        lenmsg = 1024
                        loop = (int((sssize) / lenmsg))


                        if (sssize)<lenmsg:
                            c.send(dfd[0:sssize])
                        else:
                            dddf='oksend'

                            for ioo in range(0, loop):
                                if dddf=='oksend':
                                    c.send(dfd[ioo * lenmsg:ioo * lenmsg + lenmsg])
                                    stloop = ioo * lenmsg + lenmsg
                                    print(int(m / (loop - 1) * 100))
                                    m = m + 1
                                    dddf = c.recv(6).decode()
                                    print(dddf, ioo)



                            nextloop = sssize - loop * lenmsg
                            if nextloop > 0:
                                c.send(dfd[stloop:stloop + nextloop])




                        file.close()
                        print('file transffered successfully')

Server Side:服务器端:

                        filename = input(str("Enter file name :")
                        fill = open(filename, 'wb')
                        c.send(bytes(commm, 'utf-8'))
                        # sizze=c.recv(1024).decode()
                        # print('size')
                        # print(sizze)
                        # c.send(bytes('done', 'utf-8'))
                        fileSize = c.recv(1024).decode()
                        c.send(bytes('yess', 'utf-8'))
                        lenmsg=1024

                        loop = int(int(fileSize) / lenmsg)

                        if (int(fileSize))<lenmsg:
                            image = c.recv(int(fileSize))
                            fill.write(image)
                        else:
                            for i in range(0, loop):
                                image = c.recv(lenmsg)

                                fill.write(image)
                                print('completed downloading:', int((i / (loop - 1) * 100)), '%')
                                c.send(bytes('oksend', 'utf-8'))

                            nextloop = int(fileSize) - loop * lenmsg
                            if nextloop > 0:
                                image = c.recv(nextloop)
                                fill.write(image)
                        fill.close()
                        print('file downloaded successfully')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM