473,325 Members | 2,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

XML-RPC + SimpleHTTPServer question

I'm currently implementing an XML-RPC service in Python where binary
data is sent to the server via URLs. However, some clients that need
to access the server may not have access to a web server, and I need to
find a solution. I came up with the idea of embedding a simple HTTP
server in the XML-RPC clients so that files can be sent in the
following way:

1. Start an HTTP server on the client using e.g SImpleHTTPServer on a
user allowed port
2. Call XML-RPC server with the URL to the file on the client to
upload
3. Get XML-RPC server response, then shut down HTTP server on client

Does this sound reasonable? I know that XML-RPC can send binary data,
but I presume that the URL method would be faster because the XML
parser could skip reading the binary file (is it base64 encoded as a
string like in SOAP?).

Anyway, I'm unsure of how to implement this in Python. In particular,
how do I shut down a web server once I've started it with
server_forever()? Should I run the server in a separate thread or run
it asynchronously? Since I'm only uploading one file I don't really
need to handle multiple clients; I just need to be able to shut the
server down once remote call has finished.

Any help would be appreciated since I'm new to network programming.

Jeremy

Jul 5 '06 #1
9 2952
jbrewer wrote:
I'm currently implementing an XML-RPC service in Python where binary
data is sent to the server via URLs. However, some clients that need
to access the server may not have access to a web server, and I need to
find a solution. I came up with the idea of embedding a simple HTTP
server in the XML-RPC clients so that files can be sent in the
following way:

1. Start an HTTP server on the client using e.g SImpleHTTPServer on a
user allowed port
2. Call XML-RPC server with the URL to the file on the client to
upload
3. Get XML-RPC server response, then shut down HTTP server on client

Does this sound reasonable?
why not just use an ordinary HTTP POST request ?

</F>

Jul 5 '06 #2
Fredrik Lundh wrote:
>why not just use an ordinary HTTP POST request ?
Sorry for such a simple question, but how would I do this? XML-RPC
runs on top of HTTP, so can I do a POST without running a separate HTTP
server?

Jeremy

Jul 5 '06 #3
jbrewer wrote:
Sorry for such a simple question, but how would I do this? XML-RPC
runs on top of HTTP, so can I do a POST without running a separate HTTP
server?
the XML-RPC protocol uses HTTP POST, so if you can handle XML-RPC, you
should be able to handle any POST request. what server are you using ?

</F>

Jul 5 '06 #4
>What server are you using?

Just SimpleXMLRPCServer from the standard library.

Jul 5 '06 #5
jbrewer wrote:
Just SimpleXMLRPCServer from the standard library.
which means that you should be able to do something like

from SimpleXMLRPCServer import SimpleXMLRPCServer,\
SimpleXMLRPCRequestHandler

class MyRequestHandler(SimpleXMLRPCRequestHandler):

def do_POST(self):

if self.path != "/data":
return SimpleXMLRPCRequestHandler.do_POST(self)

# handle POST to /data

bytes = int(self.headers["content-length"])

# copy 'bytes' bytes from self.rfile (in some way)
data = self.rfile.read(bytes)
# ... deal with data here ...

response = "OK"

self.send_response(200)
self.send_header("Content-type", "text/plain")
self.send_header("Content-length", str(len(response)))
self.end_headers()
self.wfile.write(response)
self.wfile.flush()
self.connection.shutdown(1)

SimpleXMLRPCServer((host, port), requestHandler=MyRequestHandler)

</F>

Jul 5 '06 #6
Fredrik Lundh wrote:
the XML-RPC protocol uses HTTP POST, so if you can handle XML-RPC, you
should be able to handle any POST request. what server are you using ?
I need some clarification of your suggestion. Instead of sending URLs,
I could read the file as a string, create a Binary object, and send
that via XML-RPC. The parameters will be sent to the server via HTTP
POST. However, the file will be encoded as a base64 string and
included in the body of the XML-RPC message, so it will have to be
parsed by the server. In my experience with SOAP, I have found this to
be extremely inefficient.

Are you suggesting sending the file separately thought a 2nd HTTP POST
with no XML-RPC message body? I guess the POST request would look
something like:

POST /path/file HTTP/1.0
From: ...
User-Agent: ...
Content-Type: /application/binary
Content-Length: <file size>

<file contents>

I'm not sure how to add a 2nd request like this. How would I alter a
simple call like that below to inlcude the 2nd post? Do I need to use
httplib and the request() method of HTTPConnection? Or can I make
POSTs through a ServerProxy object?

import xmlrpclib
server = xmlrpclib.ServerProxy("http://myserver")
result = server.my_function(file, params)

Jeremy

Jul 5 '06 #7
OK, I posted my previous message before I saw your reply on how to
handle the server side. On the client side, should I use
httplib.HTTPConnection.request() to upload the data or can I do this
through xmlrpc.ServerProxy objects?

Jeremy

Jul 5 '06 #8
I have recently implemented a system where clients connect to an RPC
server (RPyC in my case), run a webserver on the RPC server, and close
the webserver when they're done with it.

To do this I wrote a ServerThread class which wraps a SimpleHTTPServer,
runs as a thread, and can be signalled to stop. The ServerThread class
doesn't use the server_forever() method (which is just "while 1:
self.handle_request()"), instead it has a while loop which checks a
flag which is signalled from outside.

We need to set a timeout for the handle_request() call. The first thing
I tried was to use Python's socket object's new 'set_timeout' method,
but I found it caused mysterious errors to occur on my WindowsXP
machine :( Instead I call select() on the server's listening socket to
check for incoming requests, and only then call handle_request().
select()'s timeout works :)
# This is the heart of the code - the request handling loop:
while not self.close_flag.isSet():
# Use select() to check if there is an incoming request
r,w,e = select.select([self.server.socket], [], [],
self.timeout)
if r:
self.server.handle_request()

# The stop method should be called to stop the request handling loop
def stop(self, wait=False):
self.close_flag.set()
if wait:
while self.isAlive(): # isAlive() is inherited from
threading.Thread
time.sleep(self.timeout/10.0)

(in my case, self.stop_flag is a threading.Event object)
Good luck!

jbrewer wrote:
I'm currently implementing an XML-RPC service in Python where binary
data is sent to the server via URLs. However, some clients that need
to access the server may not have access to a web server, and I need to
find a solution. I came up with the idea of embedding a simple HTTP
server in the XML-RPC clients so that files can be sent in the
following way:

1. Start an HTTP server on the client using e.g SImpleHTTPServer on a
user allowed port
2. Call XML-RPC server with the URL to the file on the client to
upload
3. Get XML-RPC server response, then shut down HTTP server on client

Does this sound reasonable? I know that XML-RPC can send binary data,
but I presume that the URL method would be faster because the XML
parser could skip reading the binary file (is it base64 encoded as a
string like in SOAP?).

Anyway, I'm unsure of how to implement this in Python. In particular,
how do I shut down a web server once I've started it with
server_forever()? Should I run the server in a separate thread or run
it asynchronously? Since I'm only uploading one file I don't really
need to handle multiple clients; I just need to be able to shut the
server down once remote call has finished.

Any help would be appreciated since I'm new to network programming.

Jeremy
Jul 6 '06 #9
Thank you very much, Fredrik. Your code and suggestion worked
perfectly. I haven't benchmarked the plain HTTP post vs Binary
wrapper, but strangely even using the naive Binary wrapper in Python
sends files much faster than how Java + Axis wraps byte arrays in SOAP
messages.

Jeremy

Jul 7 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Robert J Egan | last post by:
Hi i'm trying to search a remote website page. The form returns xml information, though the page extension is missing. I retrieve the information and write it to the screen. So far so good -...
0
by: MarionEll | last post by:
Premier XML Industry Event Slated for Dec. 7-12 in Philadelphia; Presenters Include Adobe, BEA, Microsoft, IBM, Sun, Hewlett-Packard, Oracle Alexandria, Va. Sept. 30, 2003 - IDEAlliance, a...
6
by: yzzzzz | last post by:
Hi, In which cases is the <?xml version="1.0" encoding="UTF-8"?> processing instruction required at the beginning of an XML document, for the document to be valid? e.g. does it depend on the...
0
by: MarionEll | last post by:
XML 2003 Exposition Draws Leading XML Vendors Trade Show, Presentations Allow Companies to Showcase Cutting-edge Solutions Alexandria, Va. - Dec. 1, 2003 - XML 2003, the world's largest XML...
0
by: Steve Whitlatch | last post by:
It may be me, or it may be the Linux implementation of XML Catalogs on slackware. Whichever, please shed some light on this XML Catalog problem. When using the --catalogs option, xmllint resolves...
0
by: Stylus Studio | last post by:
World's Most Advanced XML Schema Editor Adds Support for IBM AlphaWorks XML Schema Quality Checker to Improve XML Schema Style and Quality BEDFORD, MA -- 09/13/2005 -- Stylus Studio...
5
by: laks | last post by:
Hi I have the following xsl stmt. <xsl:for-each select="JOB_POSTINGS/JOB_POSTING \"> <xsl:sort select="JOB_TITLE" order="ascending"/> This works fine when I use it. But when using multiple...
0
by: jts2077 | last post by:
I am trying to create a large nested XML object using E4X methods. The problem is the, the XML I am trying to create can only have xmlns set at the top 2 element levels. Such as: <store ...
10
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, I had a program and it always works fine and suddenly it gives me the following message when a pass a xml file to our server program: error code: -1072896680 reason: XML document must...
0
by: Jacker | last post by:
Xpress Author for MS Word XML Documents In.vision Research empowers knowledge workers to create complex XML documents in Microsoft Word (2000-2003) with a normal Word experience. Deploy XML...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.