简体   繁体   English

如何在Python中创建服务器端服务以处理来自Web和移动应用程序的HTTP请求?

[英]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 假设我想在服务器上运行的Python中创建服务,以

  1. receive HTTP requests from web/mobile apps 接收来自网络/移动应用的HTTP请求
  2. read data from a mid-size database 从中型数据库读取数据
  3. compute and return results in JSON 计算并以JSON返回结果

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

class BlackBox(): 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. 我做了一些研究,似乎人们对Django谈论了很多,但是我不确定这是否是一个好选择。 What are the pros and cons of Django? Django的优缺点是什么?

Answers to this question may tend to be fairly subjective but what you probably want is some sort of a web framework . 这个问题的答案可能是相当主观的,但是您可能想要的是某种Web框架 Typical features that web frameworks include: Web框架的典型功能包括:

  • Routing - Lets you easily map URLs ('www.example.com', 'www.example.com/blog') to Python code logic. 路由-使您可以轻松地将URL(“ www.example.com”,“ www.example.com/blog”)映射到Python代码逻辑。
  • Templating Engine - Lets easily get Python code into HTML pages, for generating pages based on database content, etc. 模板引擎-使Python代码轻松进入HTML页面,用于基于数据库内容生成页面等。
  • Database access - Pretty obvious. 数据库访问-很明显。 For storing your data. 用于存储数据。

django is great and very popular. django很棒而且很受欢迎。 Pyramid and Flask are also very popular. 金字塔烧瓶也很受欢迎。

I have a good amount of experience with Flask and highly recommend it. 我在Flask方面有丰富的经验,并强烈推荐它。 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. SQLAlchemy,SQLite和MongoDB是您想要研究的东西。

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 . 如果您想对可以构建在一起的每个python模块有一个很好的了解,我会说Flask If you're familiar with Python, you'll learn the basics of it in a weekend. 如果您熟悉Python,那么您将在一个周末学习它的基础知识。 It's easier that way to follow the steps from http request all the way to JSON serialization. 从http请求到JSON序列化的所有步骤都比较容易。

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. 另一方面,Django功能更全,并且已经将所有这些东西捆绑到框架中,因此您无需安装和导入它们。 it might take more time to learn it, but Django is currently the most production-ready of these two. 可能需要花费更多时间来学习它,但是Django当前是这两个产品中最适合生产的产品。

As far as things you'll need to know.. You'd have to learn some basic HTML and CSS if you haven't already. 就您需要知道的事情而言。如果您还没有学习一些基本的HTML和CSS,则必须学习一些基本的HTML和CSS 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. 在编写模板(基本上是尚未由python修改的HTML文件)时需要使用它,并且必须了解Model-View-Controller的概念,但是您将了解到您将学习框架。

But honestly, as someone mentioned before, just try it. 但是说实话,就像之前提到的那样,请尝试一下。 you'll end up learning all of this even if you don't intend to. 即使您不打算,您最终都会学到所有这些内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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