繁体   English   中英

使用XMLRPC将文件从客户端发送到服务器?

[英]Send file from client to server using XMLRPC?

我想编写Python代码以将文件从客户端发送到服务器。 服务器需要保存从客户端发送的文件。 但我的代码有一些我无法解决的错误。 以下是我的服务器代码:

# server.py
from SimpleXMLRPCServer import SimpleXMLRPCServer
import os

server = SimpleXMLRPCServer(('localhost', 9000))

def save_data(data):
    handle = open("x123.dat", "wb")
    handle.write(data)
    handle.close()

server.register_function(save_data, 'save_data')
server.serve_forever()

和客户端代码:

# client.py
import sys, xmlrpclib

proxy = xmlrpclib.Server('http://localhost:9000')
handle = open(sys.argv[1], "rb")
proxy.save_data(handle.read())
handle.close()

但后来我运行我的代码,客户端返回以下错误(这是在Windows上):

Traceback (most recent call last):
File "client.py", line 6, in <module> proxy.save_data(handle.read())
File "c:\python27\lib\xmlrpclib.py", line 1224, in __call__
  return self.__send(self.__name, args)
File "c:\python27\lib\xmlrpclib.py", line 1575, in __request
  verbose=self.__verbose
File "c:\python27\lib\xmlrpclib.py", line 1264, in request
  return self.single_request(host, handler, request_body, verbose)
File "c:\python27\lib\xmlrpclib.py", line 1297, in single_request
  return self.parse_response(response)
File "c:\python27\lib\xmlrpclib.py", line 1473, in parse_response
  return u.close()
File "c:\python27\lib\xmlrpclib.py", line 793, in close
  raise Fault(**self._stack[0])
xmlrpclib.Fault: <Fault 1: "<class 'xml.parsers.expat.ExpatError'>:not well-formed (invalid token): line 7, column 1">

我有一些疑问:

  1. 如何修复上述错误?

  2. 我的代码有时需要传输一些大文件。 由于我的方法非常简单,我怀疑移动大数据是否有效。 有人可以建议一个更好的方法来移动大文件吗? (当然最好在Python上使用XMLRPC)

服务器端:

def server_receive_file(self,arg):
        with open("path/to/save/filename", "wb") as handle:
            handle.write(arg.data)
            return True

客户端:

with open("path/to/filename", "rb") as handle:
    binary_data = xmlrpclib.Binary(handle.read())
client.server_receive_file(binary_data)

这对我有用。

您想要查看xmlrpclib 二进制对象 使用此类,您可以对base64字符串进行编码和解码。

这是你如何做到的:

#!/usr/bin/env python3.7

# rpc_server.py

# Fix missing module issue: ModuleNotFoundError: No module named 'SimpleXMLRPCServer'
#from SimpleXMLRPCServer import SimpleXMLRPCServer
from xmlrpc.server import SimpleXMLRPCServer

import os

# Put in your server IP here
IP='10.198.16.73'
PORT=64001

server = SimpleXMLRPCServer((IP, PORT))

def server_receive_file(arg, filename):
    curDir = os.path.dirname(os.path.realpath(__file__))
    output_file_path = curDir + '/' + filename
    print('output_file_path -> ({})'.format(output_file_path))
    with open(output_file_path, "wb") as handle:
        handle.write(arg.data)
        print('Output file: {}'.format(output_file_path))
        return True

server.register_function(server_receive_file, 'server_receive_file')
print('Control-c to quit')
server.serve_forever()

### rpc_client.py
#!/usr/bin/env python3.7

import os


# client.py
import sys

# The answer is that the module xmlrpc is part of python3

import xmlrpc.client

#Put your server IP here
IP='10.198.16.73'
PORT=64001


url = 'http://{}:{}'.format(IP, PORT)
###server_proxy = xmlrpclib.Server(url)
client_server_proxy = xmlrpc.client.ServerProxy(url)

curDir = os.path.dirname(os.path.realpath(__file__))
filename = sys.argv[1]
fpn = curDir + '/' + filename
print(' filename -> ({})'.format(filename))
print(' fpn -> ({})'.format(fpn))
if not os.path.exists(fpn):
    print('Missing file -> ({})'.format(fpn))
    sys.exit(1)

with open(fpn, "rb") as handle:
    binary_data = xmlrpc.client.Binary(handle.read())
    client_server_proxy.server_receive_file(binary_data, filename)

暂无
暂无

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

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