0

I'm making the jump from PHP and MySQL to Python and Google App Engine, and it's a bit different from what I'm used to. For now, I'm just trying to find out the basics of a server request.

From my client (an iPhone), I send a dictionary in JSON format with a HTTP request (it's set as the POST value).

  1. How do I get this dictionary in Python? In PHP, I would do: $dictionary=json_decode(stripslashes($_POST["dictionary"]));

    What's the Python equivalent?

Then say I run some queries, and I want to send a JSON string back to my client as a response. How would I do this in Python? Is it just self.response.out.write()? What would I put in the parenthesis to send a JSON representation of the Python dictionary?

4
  • Make sure to check Requests library which seems to be the best way to send HTTP requests in Python currently. Commented Apr 14, 2012 at 19:13
  • @PiotrDobrogost: this question has nothing to do with using Python to request something from another server. Commented Apr 14, 2012 at 20:39
  • @Wooble Really? How do you understand "sending HTTP request in Python" in the title, then? Commented Apr 14, 2012 at 20:45
  • Did you read the question? The request is being sent from an iPhone application to a python script running on App Engine. Commented Apr 14, 2012 at 21:42

2 Answers 2

3

json.

  • json.loads() is the equivalent of PHP's json_decode()
  • json.dumps() is the equivalent of PHP's json_encode()
Sign up to request clarification or add additional context in comments.

4 Comments

And what's the equivalent of $_POST["dictionary"]?
Is it self.request.get('dictionary')?
Actually, it's more like self.request.POST['dictionary']. self.request.get is a function that fetches the value from either query string parameters or request body. (That is to say, it'll work, but it will happily return a query string value instead, if there is one)
Granted, if you're relying on differentiating between the two, you may have problems.
0

If you are starting to write in Python after PHP, php2python site will help you translate some php functions to python ones.

Comments

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.