简体   繁体   中英

How do I create a server-side service in Python to handle HTTP requests from web and mobile apps?

Let's say I want to create a service in Python running on a server to

  1. receive HTTP requests from web/mobile apps
  2. read data from a mid-size database
  3. compute and return results in JSON

~~~~~~~~~~~~ Python API

class BlackBox():

   def getResults(...):
   ....

~~~~~~~~~~~~

My question is: What are other pieces I have to know?

I did some research and seems like people talked a lot about Django but I am not really sure if it would be a good choice. What are the pros and cons of Django?

Answers to this question may tend to be fairly subjective but what you probably want is some sort of a web framework . Typical features that web frameworks include:

  • Routing - Lets you easily map URLs ('www.example.com', 'www.example.com/blog') to Python code logic.
  • Templating Engine - Lets easily get Python code into HTML pages, for generating pages based on database content, etc.
  • Database access - Pretty obvious. For storing your data.

django is great and very popular. Pyramid and Flask are also very popular.

I have a good amount of experience with Flask and highly recommend it. It doesn't include database support directly, but that way you can choose whichever you want. SQLAlchemy, SQLite, and MongoDB would be things you'd want to look in to.

if you want to get a good idea of every python module that you can put together to build this, I would say go with Flask . If you're familiar with Python, you'll learn the basics of it in a weekend. It's easier that way to follow the steps from http request all the way to JSON serialization.

Django, on the other hand, is more full-featured and already comes with all of these things bundled into the framework, so you wont have to install and import them. it might take more time to learn it, but Django is currently the most production-ready of these two.

As far as things you'll need to know.. You'd have to learn some basic HTML and CSS if you haven't already. You'll need it for when you write templates (which are basically HTML files that haven't been modified by python yet) and you'll have to understand the concept of a Model-View-Controller, but you'll learn that as you learn the framework.

But honestly, as someone mentioned before, just try it. you'll end up learning all of this even if you don't intend to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM