1

I am programming a online Expression Calculator in GAE using Python. I am using query string to get the query and then evaluate the expression. For example for ishamsample.appspot.com/eval?q=9-6 browser should show {9-3}{6} This like query is working properly but the problem is + symbol. ishamsample.appspot.com/eval?q=1+6 Below is my code. I tried URL quoting

class Eval(webapp2.RequestHandler):
    def get(self):
        q=self.request.get('q')
        q=urllib.quote(q)
        code=eval(compile(q,'<string>', 'eval', __future__.division.compiler_flag))
        self.response.write('{'+q+'}{'+str(code)+'}')

For that browser shows output as {1%206}{1} enter image description here

How to overcome this issue.

Isham Mohamed
  • 2,629
  • 1
  • 14
  • 27

2 Answers2

1

You should not encode the string you get as GET parameter on the server side. You should be decoding it; and GAE does that for you. Whoever is the client (here: you as a user) should encode the symbols before making an HTTP request.

Your request should look like:

http://.../?q=1%2B6

One comment that I cant resist making: EVAL IS EVIL. Please refer to a similar question. on how to do it using ast.literal_eval(..).

Community
  • 1
  • 1
UltraInstinct
  • 43,308
  • 12
  • 81
  • 104
0
  • is a special symbol, it is supposed to replace spaces. In the url you should have %2B. If you post the query from a text input it should be encoded naturally. Nothing to do with python or GAE
Thomas
  • 8,306
  • 8
  • 53
  • 92
  • Nope in URL, users put + sign and thats why I need URL Encoding here. For example, if a user put `ishamsample.appspot.com/eval?q=1+6 ` it should encode `+` to `%2B` and give result as `{1+6}{7}`. Most of the users dont know the URL value for + thus we have to encode it. – Isham Mohamed Nov 15 '13 at 14:19
  • @IshamMohamed He is actually correct in what he is saying. So a +1 from my side. – UltraInstinct Nov 15 '13 at 14:28