Calling a java servlet from Python

Hi,

I'm trying to write some python to send a request to my java servlet. I haven't written python before and I'm having problems finding examples like this.

What I'm trying to do is call my servlet with my params and read the params from the responce.

If you've done something like this, I'd really appreciate your help!

Thanks,
Derek.
LVL 1
ruamireAsked:
Who is Participating?
 
cjjcliffordConnect With a Mentor Commented:
The module "urllib" provides URL access mechanisms:

import urllib
a = urllib.urlopen( 'http://www.experts-exchange.com' )
lines = a.readlines()
a.close()

This will return you all the HTML text from that URL.

If your Java servlet is expecting a HTTP POST with parameters these can be added as follows:

params = urllib.urlencode( { "name" : "value", "name2" : "value2" } )
a = urllib.urlopen( 'http://www.experts-exchange.com', data=params )

Hope this helps!

Cheers,
C.
0
 
ruamireAuthor Commented:
Hey,

That looks nice enough, I'll give it a go and let you know how I get on.

Cheers,
D.
0
 
ruamireAuthor Commented:
Hi,

I've done as suggested but I'm getting an error, it's assuming this is a POST request as opposed to a GET. Is there a way to specify what kind of request it is?

Thanks,
Derek.
0
 
ruamireAuthor Commented:
I figured it out. Put in this instead....


      a = urllib.urlopen( 'http://localhost:8080/DebitRequest/DebitRequest?'+params )

Thanks for your help.

Derek.
0
 
cjjcliffordCommented:
glad to be of help :-)
0
All Courses

From novice to tech pro — start learning today.