Python套接字编程设计和实现:将文件发送到服务器,服务器计算,接收文件 [英] Python socket programming design and implementation: send file to server, server compute, receive file back
问题描述
这是我当前的算法。
客户端站点:
- 初始化套接字
- 将文件发送到服务器
- 从服务器接收文件
- 关闭套接字
- initialize socket
- send file to server
- receive file from server
- close socket
服务器站点:
- 初始化套接字并监听
现在在每个请求的新线程中: - 从客户端接收文件
- 计算机生成的文件(40秒操作)
- 发送结果文件
- 关闭套接字
- 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
对于实现,我使用带套接字的Python和线程模块,如此教程所示,然后我重复发送/
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.
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
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')
取决于输入文件 client_received_file
比预期的要小,而 server_received_file
始终为空。
Depending on the input files, the client_received_file
is a bit smaller than expected whereas the server_received_file
is always empty.
推荐答案
您应在监听之前绑定服务器。
You should bind your server before listening.
请参见 https://docs.python.org/2/howto/sockets.html
这篇关于Python套接字编程设计和实现:将文件发送到服务器,服务器计算,接收文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!