Python socket programming design and implementation: send file to server, server compute, receive file back

别说谁变了你拦得住时间么 提交于 2019-12-11 16:23:18

问题


Here's my current algorithm.

Client site:

  • initialize socket
  • send file to server
  • receive file from server
  • close socket

Server site:

  • initialize socket and listen Now in a new thread per request:
  • receive file from client
  • computer resulting file (40 second operation)
  • send resulting file
  • close socket

For implementation I'm using Python with socket and threading modules, as shown in this tutorial, then I'm repeating the send/receive code on the other sides.

That's my first server/client development experience that involves moving large files. If the design is bad or a different architecture is advised or you have a better tutorial/documents I can use, let me know.

server.py

import socket
from threading import Thread

TCP_IP = 'localhost'
TCP_PORT = 9001
BUFFER_SIZE = 1024

class ClientThread(Thread):

    def __init__(self,ip,port,sock):
        Thread.__init__(self)
        self.ip = ip
        self.port = port
        self.sock = sock
        print (" New thread started for "+ip+":"+str(port))

    def run(self):
        filename='mytext.txt'
        f = open(filename,'rb')
        while True:
            l = f.read(BUFFER_SIZE)
            while (l):
                self.sock.send(l)
                #print('Sent ',repr(l))
                l = f.read(BUFFER_SIZE)
            if not l:
                f.close()
                # self.sock.close()
                break

        with open('server_received_file', 'wb') as f:
            print('file opened')
            while True:
                #print('receiving data...')
                data = self.socks.recv(BUFFER_SIZE)
                print('data=%s', (data))
                if not data:
                    f.close()
                    print ('file close()')
                    break
                # write data to a file
                f.write(data)


tcpsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tcpsock.bind((TCP_IP, TCP_PORT))
threads = []

while True:
    tcpsock.listen(5)
    print ("Waiting for incoming connections...")
    (conn, (ip,port)) = tcpsock.accept()
    print ('Got connection from ', (ip,port))
    newthread = ClientThread(ip,port,conn)
    newthread.start()
    threads.append(newthread)

for t in threads:
    t.join()

client.py

import socket

TCP_IP = 'localhost'
TCP_PORT = 9001
BUFFER_SIZE = 1024

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
with open('client_received_file', 'wb') as f:
    print ('file opened')
    while True:
        #print('receiving data...')
        data = s.recv(BUFFER_SIZE)
        print('data=%s', data)
        if not data:
            f.close()
            print ('file close()')
            break
        # write data to a file
        f.write(data)


filename='result.txt'
f = open(filename,'rb')
while True:
    l = f.read(BUFFER_SIZE)
    while (l):
        s.send(l)
        #print('Sent ',repr(l))
        l = f.read(BUFFER_SIZE)
    if not l:
        f.close()
        s.close()
        break

print('Successfully get the file')
s.close()
print('connection closed')

Depending on the input files, the client_received_file is a bit smaller than expected whereas the server_received_file is always empty.


回答1:


You should bind your server before listening.

See https://docs.python.org/2/howto/sockets.html



来源:https://stackoverflow.com/questions/46615493/python-socket-programming-design-and-implementation-send-file-to-server-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!