简体   繁体   中英

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. For example, when I send "start" command to the server, it will execute job_function, but during this time, the client is freezing. 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:

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:

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.

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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