简体   繁体   English

在服务器端使用Python和平面文件数据库

[英]Using Python and flat file database for server-side

I don't have access PHP server nor database like Mysql on machine I'll be working on. 我将无法使用的机器上无法访问PHP服务器或Mysql之类的数据库。 Would it be feasible to use Python instead of PHP and flat file database instead of Mysql? 使用Python代替PHP以及使用平面文件数据库代替Mysql是否可行? I'm not too concerned about performance or scalability. 我不太担心性能或可伸缩性。 It's not like I'm going to create next facebook. 这不像我要创建下一个Facebook。 I just want to load data from server and show it on webpage and possibly handle some input forms. 我只想从服务器加载数据并将其显示在网页上,并可能处理一些输入形式。 Also is there any major flaw with my reasoning? 我的推理还有什么重大缺陷吗? Or is there any other way to circumvent lack of PHP and database on server? 还是有其他方法可以避免服务器上缺少PHP和数据库?

Using Python on a server is a longer story, because Python and PHP are not comparable. 在服务器上使用Python的故事由来已久,因为Python和PHP不具有可比性。 Have a look at Python + Django, and then think of what you want to do. 看一下Python + Django,然后想想要做什么。 See: https://www.djangoproject.com 请参阅: https//www.djangoproject.com

Writing server-side scripts with Python is not difficult, and the WSGI interface works well. 用Python编写服务器端脚本并不难,并且WSGI接口运行良好。 However, you usually need several layers of software if you want to get PHP-equivalent functionality. 但是,如果要获得与PHP等效的功能,通常需要多层软件。

The second question - database - is easier. 第二个问题-数据库-比较容易。 Python comes with SQLite3 interface, so there you have a quite-SQLish database with only a single DB file. Python带有SQLite3接口,因此那里有一个相当SQLish的数据库,只有一个DB文件。 SQLite is very widely used and usually reliable. SQLite被广泛使用并且通常可靠。 For more information on the sqlite3 modeule, see: https://docs.python.org/2/library/sqlite3.html . 有关sqlite3模块的更多信息,请参见: https ://docs.python.org/2/library/sqlite3.html。

Python comes bundled with sqlite3 module that gives access to SQLite databases. Python与sqlite3模块捆绑在一起,该模块提供对SQLite数据库的访问。 The only downside is that it is pretty much possible for just one thread can have write locks to it at any given moment. 唯一的缺点是,很可能只有一个线程可以在任何给定时刻对其进行写锁定。

There are many ways to serve Python applications, but you should probably look at something that does this using the WSGI standard. 有许多服务Python应用程序的方法,但是您可能应该看看使用WSGI标准执行此操作的方法。 Many frameworks will let you do this eg: Pyramid, Pylons, Django, ..... If you haven't picked one then it would be worth looking at your long term requirements and also what you already know. 许多框架都可以使您做到这一点,例如:Pyramid,Pylons,Django,......如果您还没有选择,那么值得考虑您的长期需求以及已经知道的知识。

In terms of DB, there are many choices. 在数据库方面,有很多选择。 SQLlite has been mentioned, but there are many other DBs that don't require a server process. 已经提到了SQLlite,但是还有许多其他不需要服务器进程的数据库。 If you're only storing a small amount of data then flat files may work for you, but anything bigger or more relational, then look at SQLlite. 如果您只存储少量数据,那么平面文件可能对您有用,但是任何更大或更相关的文件,请查看SQLlite。

If you're looking to get into Python for web development, take a look at the Flask framework. 如果您打算使用Python进行Web开发,请查看Flask框架。

There are numerous examples where a flat file is used to serve data, rather than a database. 有许多示例使用平面文件而不是数据库来提供数据。

See the blog post by James Sharding which discusses how to use Flask-FlatPages to serve content from files. 请参阅James Sharding的博客文章 ,其中讨论了如何使用Flask-FlatPages来提供文件内容。

You can easily adapt this to serve straight text (rather than parse it as Markdown), as well. 您也可以轻松地调整它以提供纯文本(而不是将其解析为Markdown)。

The end result is a lean, robust system that avoids the overhead of a database. 最终结果是精简,强大的系统,避免了数据库的开销。

As the other answers suggest, using SQLite is another great option. 正如其他答案所暗示的那样,使用SQLite是另一个不错的选择。

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

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