?
Solved

Calling a java servlet from Python

Posted on 2005-03-03
5
Medium Priority
?
835 Views
Last Modified: 2012-06-27
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.
0
Comment
Question by:ruamire
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 11

Accepted Solution

by:
cjjclifford earned 1000 total points
ID: 13448447
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
 
LVL 1

Author Comment

by:ruamire
ID: 13448501
Hey,

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

Cheers,
D.
0
 
LVL 1

Author Comment

by:ruamire
ID: 13451434
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
 
LVL 1

Author Comment

by:ruamire
ID: 13451478
I figured it out. Put in this instead....


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

Thanks for your help.

Derek.
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 13456880
glad to be of help :-)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question