Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
20.9.2014 - We have just updated our course layouts and descriptions and added our 2015 schedule.

Jython:How  to replace parameters in URL path?

Posted by patni (patni), 14 May 2005
Following is the URL
self.http.setUrl( '''http://192.168.135.251/logapp20/app/report/realtime/customreportexecute?operation=run&reportId=26
&reportType=ipConnections&reportName=Test_Custom_Report'''.replace('192.168.135.251', location))


How I can replace parameters in URL
For example
Replace ipaddress<192.168.135.251> to another ipaddress
and
Replace reportId=26 to reportId=repid
repid is the variable which has some value


Posted by admin (Graham Ellis), 14 May 2005
Here's code that worked for me:

Code:
template = '''http://192.168.135.251/logapp20/app/report/realtime/customreportexecute?operation=run&reportId=26&reportType=ipConnections&reportName=Test_Custom_Report'''

newlocation = "10.20.30.40"
repid = "778"

becomes = template.replace('192.168.135.251', newlocation).replace('reportId=26','reportID='+repid)

print becomes


A couple of things I noticed.   Firstly, you had some spurious spaces (but that might just have been to do with cutting and pasting onto the forum.  Secondly, you didn't say whether your repid was a string or a number ... if it was a number, you would need to add a call to the str function to convert it in the second part of the replace.

I have chosen to replace rather more than just the "26" in the second part of the question - I'm fearful of the string 26 occurring elsewhere in the input and I wanted to make it a little more robust.

Hope this answers the question ... if not, please post up what you actually get out (i.e. error messages or wrong information) and what you were hoping for, and I'll see if I can give you some further pointers.

Posted by admin (Graham Ellis), 16 May 2005
I am tidying up ... the thread appears to have broken into two ... here is the further response from the original poster

Thanks Graham Ellis

I tried with your solution but still its giving me
NameError: repid

'repid' is the number and I will take it from one txt file.

#Code to get repid from file in file system
f =  open ( '/root/sqatest/test_suites/scripts/avadhoot_scripts/id.txt', 'r' )
repid = f.read()
f.close()
print repid


#Changes I did in the script.

template = '''http://192.168.135.251/logapp20/app/report/realtime/customreportexecute?operation=run&reportId=26&reportType=ipConnections&reportName=Test_Custom_Report'''

becomes = template.replace('192.168.135.251', location).replace('reportId=26','reportId='+repid)


print becomes
http.setUrl( becomes )


After executing it gives error
NameError: repid

Posted by admin (Graham Ellis), 16 May 2005
Where did the error occur?   Did it print anything else out?



Posted by patni (patni), 16 May 2005
error occurs at
Traceback (innermost last):
 File "/root/sqatest/test_suites/scripts/avadhoot_scripts/testagents/LX_CustomReport_Save.a", line 219, in ?
 File "/root/sqatest/test_suites/scripts/avadhoot_scripts/testagents/LX_CustomReport_Save.a", line 133, in run
NameError: repid


my script's line 133 is

becomes = template.replace('192.168.135.251', location).replace('reportId=26','reportId='+repid)

Posted by admin (Graham Ellis), 16 May 2005
And did the
print repid
just a few lines above print anything out?

I'm guessing that the two pieces of code you have shown me are in different functions or namespaces, so that the repid name isn't available at the call to the replace method

Posted by patni (patni), 16 May 2005
Your guess was right.
Thanks

but now its giving

http://192.168.135.251/logapp20/app/report/realtime/customreportexecute?operation=run&reportId=2
&reportType=ipConnections&reportName=Test_Custom_Report
Traceback (innermost last):
 File "/root/sqatest/test_suites/scripts/avadhoot_scripts/testagents/LX_CustomReport_Save.a", line 216, in ?
 File "/root/sqatest/test_suites/scripts/avadhoot_scripts/testagents/LX_CustomReport_Save.a", line 133, in run
NameError: http


Posted by admin (Graham Ellis), 16 May 2005
... which is because you don't have anything called http in scope ...

Posted by patni (patni), 16 May 2005
I am very new to Jython.

So please tell me How I can do that?

Posted by admin (Graham Ellis), 16 May 2005
.... in other words, you're probably missing a line like ...

http =

to set up the variable.

In Python and Jython, if you try to use any object before you have created it, you get a name error.   Than can be because you haven't set up the variable, or because the variable only exists in another "scope" as per the problem you were having earlier in this thread.


Posted by patni (patni), 16 May 2005
Now problem is


http://192.168.135.251/logapp20/app/report/realtime/customreportexecute?operation=run&reportId=2
&reportType=ipConnections&reportName=Test_Custom_Report
Encountered an error: com.pushtotest.tool.UnsupportedTypeException: Illegal character in query at index 96 http://192.168.135.251/logapp20/app/report/realtime/customreportexecute?operation=run&reportId=2
&reportType=ipConnections&reportName=Test_Custom_Report


Posted by admin (Graham Ellis), 16 May 2005
Yep, that's probably because your repid has a new line character on the end of it, and a new line character isn't allowed in a URL.

You can get rid of spurious white space on string objects by using the strip method:

newname = oldname.strip()

or in your case

repid = repid.strip()


Posted by patni (patni), 16 May 2005
Thanks a lot
It is working



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho