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.

0

2 Answers 2

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(..).

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

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.
@IshamMohamed He is actually correct in what he is saying. So a +1 from my side.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.