在客户端和服务器之间通过python套接字编程发送多个图像

时间:2019-04-11 15:59:02

标签: python-2.7

我一直试图通过python2.7中的套接字编程发送多个图像。如果将主机设置为localhost,则代码可以正常工作,但是当我尝试将客户端和服务器放在不同的计算机上时,将传输一个图像,然后出现以下错误:socket.error:[Errno 32]管道损坏。我认为问题在于在server.py文件中放置sock.shutdown(),但是我不知道该在哪里放置它。任何帮助将不胜感激!

如上所述,当主机设置为127.0.0.1时,代码可以正常工作,但是如果将其设置为192.168.0.100(服务器地址),则会发生上述问题。

client.py:

cwd = os.getcwd()

HOST = '192.168.0.100'
PORT = 6666

def transmit():
    for filename in os.listdir(cwd): # Current working directory
        if filename.endswith(".jpg") or filename.endswith(".png"):
            try:
                sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                server_address = (HOST, PORT)
                sock.connect(server_address)

                # open image
                myfile = open(filename, 'rb')
                bytes = myfile.read()
                size = len(bytes)

                # send image size to server
                sock.sendall("SIZE %s" % size)
                answer = sock.recv(4096)

                print ('answer = %s' % answer)

                # send image to server
                if answer == 'GOT SIZE':
                    sock.sendall(bytes)

                    # check what server send
                    answer = sock.recv(4096)
                    print ('answer = %s' % answer)

                    if answer == 'GOT IMAGE' :
                        print ('Image successfully send to server')

                myfile.close()

            except:
                sock.close()
                continue  
        else:
            # time.sleep(10)
            continue

server.py:

HOST = '192.168.0.100'
PORT = 6666

connected_clients_sockets = []

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((HOST, PORT))
server_socket.listen(10)

connected_clients_sockets.append(server_socket)

def receive():
    print("Receive")
    imgcounter = 1
    basename = "image%s.jpg"
    while True:

        read_sockets, write_sockets, error_sockets = select.select(connected_clients_sockets, [], [])

        for sock in read_sockets:

            if sock == server_socket:

                sockfd, client_address = server_socket.accept()
                connected_clients_sockets.append(sockfd)

            else:
                try:

                    data = sock.recv(4096)
                    txt = str(data)

                    if data:

                        if data.startswith('SIZE'):
                            tmp = txt.split()
                            size = int(tmp[1])

                            print ('got size')

                            sock.sendall("GOT SIZE")

                        else :

                            myfile = open(basename % imgcounter, 'wb')
                            myfile.write(data)

                            data = sock.recv(40960000)
                            if not data:
                                myfile.close()
                                break
                            myfile.write(data)
                            myfile.close()

                            sock.sendall("GOT IMAGE")
                            sock.shutdown()
                except:
                    sock.close()
                    connected_clients_sockets.remove(sock)
                    continue
            imgcounter += 1
    server_socket.close()

0 个答案:

没有答案