Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
344 lines (328 loc) · 19.5 KB
/
Copy pathindex.html
File metadata and controls
344 lines (328 loc) · 19.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<!doctype html>
<!--[if lt IE 7]> <html class="no-js ie6 oldie" lang="en"> <![endif]-->
<!--[if IE 7]> <html class="no-js ie7 oldie" lang="en"> <![endif]-->
<!--[if IE 8]> <html class="no-js ie8 oldie" lang="en"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js" lang="en"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="language" content="English" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Full Stack Python</title>
<meta name="description" content="Full stack Python shows how an entire Python web application is built, from the server to the front end JavaScript.">
<meta name="author" content="Matt Makai">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="stylesheet" href="/css/c.css" />
<link rel="stylesheet" href="/css/bootstrap-responsive.min.css" />
<script src="/js/modernizr-2.0.6.min.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-19910497-7']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body data-spy="scroll" data-target="#fsp-side-nav" >
<div class="container">
<div class="row">
<div class="span12">
<div class="logo-header-section">
<a href="/"><img src="../img/full-stack-python-logo-bw.png" height="42" width="42" class="logo-image" /></a>
<span class="logo-title"><a href="/">Full Stack Python</a></span>
</div>
</div>
</div>
<div class="row">
<div class="span2" id="fsp-side-nav">
<ul class="nav nav-list fsp-side-nav-ul affix">
<li><a href="#introduction">Introduction</a></li>
<li><a href="#servers">Servers</a></li>
<li><a href="#operating-system">Operating System</a></li>
<li><a href="#web-server">Web Server</a></li>
<li><a href="#database">Database</a></li>
<li><a href="#web-framework">Web Framework</a></li>
<li><a href="#wsgi-server">WSGI Server</a></li>
<li><a href="#content-delivery-network">Content Delivery Network</a></li>
<li><a href="#change-log">Change Log</a></li>
</ul>
</div>
<div class="span9 offset1">
<section id="introduction" class="tech-section">
<h1>Introduction</h1>
<p>You're knee deep in learning the <a class="reference external" href="http://www.python.org/">Python</a>
programming language. The syntax is beginning to make sense. The first
few "AHA!" moments are hitting you. Now you are excited for direction to learn
more.</p>
<p>Now what? Definitely read Kenneth Reitz's
<a class="reference external" href="http://docs.python-guide.org/en/latest/">The Hitchhiker’s Guide to Python</a>
if you have not already. If you're not coming from a development
background, you may find this post on
<a class="reference external" href="http://www.mattmakai.com/learning-python-for-non-developers.html">Learning Python</a>
useful.</p>
<p>This guide has a different focus from the above resources. Here I
focus on explaining several ways of setting up a Python <em>web application</em>
stack from server infrastructure through JavaScript execution on a user's
browser.</p>
<div class="section" id="about-the-author">
<h2>About the Author</h2>
<p>I'm <a class="reference external" href="http://www.mattmakai.com/">Matt Makai</a> and I solve problems by
developing software. I often use Python and occasionally work with
languages in the Java Virtual Machine (JVM) stack.</p>
<p>In 2013 I will be traveling to 30 cities in the United States to visit with
tech companies, attend and speak at tech meetups, and blog from an experienced
software developer's perspective about the current state of technology in the
United States. If you're reading this guide then you're likely someone I'm
interested in meeting during my trip. Send me an email at
<a class="reference external" href="mailto:matthew.makai@gmail.com">matthew.makai@gmail.com</a> or
<a class="reference external" href="https://twitter.com/makaimc">message me on Twitter</a>. Read more about
my upcoming trip that will take place between March 9 and August 15, 2013 at
<a class="reference external" href="http://www.codingacrossamerica.com/about.html">Coding Across America</a>.</p>
<p>If there are typos, inaccurate statements, or general areas for improvement
that you see in this guide, please issue a
<a class="reference external" href="https://github.com/makaimc/fullstackpython.github.com/pull/new/gh-pages">pull request on this website's Github repository</a>. Thank you!</p>
</div>
</section>
<section id="servers" class="tech-section">
<h1>Servers</h1>
<p>There are several options for setting up infrastructure to serve a
web application:</p>
<ol class="arabic simple">
<li>"Bare metal" servers</li>
<li>Virtualized servers</li>
<li>Infrastructure-as-a-service</li>
<li>Platform-as-a-service</li>
</ol>
<div class="section" id="bare-metal">
<h2>Bare metal</h2>
<p>The term <em>bare metal</em> refers to purchasing the actual hardware and hooking
it up to the Internet either through a business-class internet service
provider (ISP) or
<a class="reference external" href="http://webdesign.about.com/od/colocation/a/what_colocation.htm">co-locating the server</a> with other servers.</p>
<p>The bare metal option offers the most control over the server configuration,
usually has the highest performance for the price, but also is the most
expensive upfront option and the highest ongoing maintenance. With bare
metal servers the ongoing operating cost is the electricity the server(s)
use as well as handling repairs when server components malfunction.</p>
<p>Buy actual hardware from a vendor either pre-built or as a collection of components that you assemble yourself.</p>
</div>
<div class="section" id="virtualized-servers">
<h2>Virtualized servers</h2>
<p>Virtual private servers (VPSs) are slices of hardware on top of a larger
bare metal server. Virtualization software such as
<a class="reference external" href="http://www.xen.org/">Xen</a> and
<a class="reference external" href="http://www.vmware.com/virtualization/what-is-virtualization.html">VMWare</a>
allow providers such as <a class="reference external" href="http://www.linode.com/">Linode</a> and
<a class="reference external" href="http://prgmr.com/xen/">prgmr</a> (as well as a many others) to provide
fractions of a full server that appear as their own instances. For example,
a server with an 8-core Xeon processor and 16 gigabytes of memory can be
sliced into 8 pieces with the equivalent of 1-core and 2 gigabytes of
memory.</p>
<p>The primary disadvantage of virtualized servers is that there is resource
overhead in the virtualization process. In addition, physical constraints
such as heavy I/O operations by a single virtualized instance on persistent
storage can cause performance bottlenecks for other virtualized instances on
the shared server. Choosing virtualized server hosting should be based on
your needs for urgency of service ticket requests and the frequency you
require for ongoing maintenance such as persistent storage backups.</p>
</div>
<div class="section" id="infrastructure-as-a-service">
<h2>Infrastructure-as-a-service</h2>
<p>Infrastructure-as-a-service (IaaS) overlaps with virtualized servers
because the resources are often presented in the same way. The
difference between virtualized servers and IaaS is the granularity of the
billing cycle. IaaS generally encourages a finer granularity based on minutes
or hours of server usage instead of on monthly billing cycles.</p>
<p>IaaS can be used in combination with virtualized servers to provide
dynamic upscaling for heavy traffic. When traffic is low then virtualized
servers can solely be used. This combination of resources reduces cost at
the expense of greater complexity in the dynamically scaled infrastructure.</p>
</div>
<div class="section" id="platform-as-a-service">
<h2>Platform-as-a-service</h2>
<p>Platform-as-a-service (PaaS) refers to a class of services that will host
an application with a layer between the server and the application. The
layer defines how the application accesses resources such as computing
time, files, and external services. The tradeoff to PaaS is usually
ease of deployment to a specific architecture as long as you do things
"their way."</p>
<p>If you go the PaaS route, you can skip over the operating system and web
server sections because they are baked into PaaS offerings.</p>
</div>
<div class="section" id="server-monitoring">
<h2>Server Monitoring</h2>
<p>There are several important resources to monitor on the server level of a web
stack. Technically some of these cross into the operating system level (next
section) but they are listed here:</p>
<ol class="arabic simple">
<li>Server uptime</li>
<li>CPU utilization</li>
<li>Memory utilization</li>
<li>Persistence storage consumed versus free</li>
<li>Network bandwidth and latency</li>
</ol>
</div>
<div class="section" id="server-resources">
<h2>Server Resources</h2>
<p><a class="reference external" href="http://arstechnica.com/gadgets/2012/11/how-to-set-up-a-safe-and-secure-web-server/">How to set up a safe and secure Web server</a> (Ars Technica)</p>
<p><a class="reference external" href="http://www.kalzumeus.com/2010/12/12/staging-servers-source-control-deploy-workflows-and-other-stuff-nobody-teaches-you/">Staging Servers, Source Control & Deploy Workflows, And Other Stuff Nobody Teaches You</a> (Patrick McKenzie)</p>
<p><a class="reference external" href="http://blog.redfern.me/choosing-a-low-cost-vps/">Choosing a low cost VPS</a>
(Joe's Blog)</p>
</div>
</section>
<section id="operating-system" class="tech-section">
<h1>Operating System</h1>
<p>The operating system sits directly on top of the server or virtual server
and provides access to computing resources. The only operating
system I can recommend for production Python web stack deployments is
a flavor of Linux. Ubuntu Long Term Support (LTS) releases, Red Hat
Enterprise Linux, and CentOS are all viable options.</p>
<p><em>Side note</em>: Mac OS X is fine for development activities but not for
test/production deployments.</p>
<div class="section" id="ubuntu">
<h2>Ubuntu</h2>
<p>Ubuntu is a Linux distribution packaged by the
<a class="reference external" href="http://www.canonical.com/">Canonical Ltd</a> company. Ubuntu uses the
Debian distribution as a base for packages, including the <a class="reference external" href="http://wiki.debian.org/Apt">aptitude package
manager</a>. For desktop versions of Ubuntu,
GNOME (until the 11.04 release) or Unity (11.10 through current)
is added to the distribution to provide a user interface.</p>
</div>
<div class="section" id="red-hat-and-centos">
<h2>Red Hat and CentOS</h2>
<p>Red Hat Enterprise Linux (RHEL) and Community enterprise Operating
System (CentOS) are essentially the same distribution. The key
difference is that CentOS is an open source free derivative of RHEL.</p>
</div>
</section>
<section id="web-server" class="tech-section">
<h1>Web Server</h1>
<p>A web server handles Hypertext Transfer Protocol (HTTP) requests from
clients. The client is usually a browser such as Internet Explorer, Firefox,
and Chrome, but it can also be a headless browser like
<a class="reference external" href="http://phantomjs.org/">phantomjs</a> (commonly used for testing), a
commandline utility like wget or curl, or a web crawler. The web server
processes the request and sends a status code and depending on the
status code a content response as well.</p>
<p>In a simple case, the client will request a static asset such as a picture
or JavaScript file. The file sits on the file system in a location the
web server is authorized to access and the web server sends the file
to the clinet with a 200 status code. If the client already requested the
file and the file has not changed, the web server will pass back a 304
"Not modified" response indicating the client already has the latest version
of that file.</p>
<p>Sending static assets can eat up a large amount of bandwidth which is why
using a Content Delivery Network (CDN) is important when possible (see the
content delivery network section for a more detailed explanation).</p>
</section>
<section id="database" class="tech-section">
<h1>Database</h1>
<p>A database provides an abstraction on top of a file system for creating,
reading, updating, and deleting persistent data. The abstraction most
commonly represented is a set of relational tables, known as a
relational database management system (RDBMS). Alternative abstractions
include graph databases where data is stored in both nodes and edges of a
graph.</p>
<div class="section" id="database-resources">
<h2>Database resources</h2>
<p><a class="reference external" href="http://www.postgresql.org/">PostgreSQL</a> and
<a class="reference external" href="http://www.mysql.com/">MySQL</a> are two of the most common open source
databases.</p>
<p><a class="reference external" href="http://www.sqlite.org/">SQLite</a> is a database that is stored in a single
file on disk. SQLite is built into Python but is only built for access
by a single connection at a time.</p>
</div>
</section>
<section id="web-framework" class="tech-section">
<h1>Web Framework</h1>
<p>A web application framework is a collection of libraries that
provide functionality to accomplish common operations for the web. These
common operations include:</p>
<ol class="arabic simple">
<li>URL routing</li>
<li>HTML, XML, JSON, and other output templating</li>
<li>Database manipulation</li>
<li>Cross-site request forgery (CSRF) and Cross-site scripting (XSS) protection</li>
</ol>
<p>Not all web frameworks include functionality for all of the above
functionality. Frameworks must balance between "being all things to all
people but very complicated" or doing only certain things well without
prescription for how to do other functions.</p>
<p>For example, the Django web application framework includes an
Object-Relational Mapping (ORM) layer that abstracts relational database
read, write, query, and delete operations. However, the ORM layer in Django
does not work (without modification) on non-relational databases such
<a class="reference external" href="http://www.mongodb.org/">MongoDB</a> and <a class="reference external" href="http://docs.basho.com/">Riak</a>.
Other web frameworks such as Flask and Pyramid are generally easier to
use with non-relational databases by incorporating external Python libraries.</p>
<div class="section" id="web-framework-resources">
<h2>Web Framework Resources</h2>
<p><a class="reference external" href="http://www.djangoproject.com/">Django</a>,
<a class="reference external" href="http://flask.pocoo.org/">Flask</a>,
<a class="reference external" href="http://bottlepy.org/docs/dev/">Bottle</a>,
<a class="reference external" href="http://www.pylonsproject.org/">Pyramid</a>, and
<a class="reference external" href="http://webpy.org/">web.py</a> are the most common Python web frameworks.</p>
</div>
</section>
<section id="wsgi-server" class="tech-section">
<h1>WSGI Server</h1>
<p>A <a class="reference external" href="http://wsgi.readthedocs.org/en/latest/">Web Server Gateway Interface</a>
(WSGI) server implements the web server side of the WSGI interface for
running Python web applications. The WSGI standard v1.0 is specified in
<a class="reference external" href="http://www.python.org/dev/peps/pep-0333/">PEP 0333</a>. As of September 2010,
WSGI v1.0 is superseded by
<a class="reference external" href="http://www.python.org/dev/peps/pep-3333/">PEP 3333</a>, which defines the
v1.0.1 WSGI standard.</p>
<div class="section" id="wsgi-resources">
<h2>WSGI Resources</h2>
<p><a class="reference external" href="http://www.python.org/dev/peps/pep-0333/">PEP 0333 WSGI v1.0</a>
and
<a class="reference external" href="http://www.python.org/dev/peps/pep-3333/">PEP 3333 WSGI v1.0.1</a>
specifications.</p>
<p><a class="reference external" href="http://gunicorn.org/">Green Unicorn</a>,
<a class="reference external" href="http://code.google.com/p/modwsgi/">mod_wsgi</a>,
<a class="reference external" href="https://github.com/unbit/uwsgi-docs">uWSGI</a>, and
<a class="reference external" href="http://www.gevent.org/">gvent</a> are common WSGI server implementations.</p>
</div>
</section>
<section id="content-delivery-network" class="tech-section">
<h1>Content Delivery Network</h1>
<p>A content delivery network (CDN) serves static file assets.
<a class="reference external" href="http://aws.amazon.com/cloudfront/">Amazon CloudFront</a>,
<a class="reference external" href="http://www.akamai.com/">Akamai</a>, and
<a class="reference external" href="http://www.rackspace.com/cloud/public/files/">Rackspace Cloud Files</a>
are examples of CDNs. The purpose of a CDN is to remove the load of static
file requests from web servers that are handling dynamic web content. For
example, if you have an nginx server that handles both static files and
acts as a front for a Green Unicorn WSGI server on a 512 megabyte
virtual private server, the nginx server will run into resource
constraints under heavy traffic. A CDN can remove the need to serve static
assets from that nginx server so it can purely act as a pass through for
requests to the Green Unicorn WSGI server.</p>
<p>CDNs distribute request load globally by using data centers in different
locations.</p>
<div class="section" id="cdn-resources">
<h2>CDN Resources</h2>
<p><a class="reference external" href="http://blog.doismellburning.co.uk/2012/07/14/using-amazon-s3-to-host-your-django-static-files/">Using Amazon S3 to host your Django static files</a></p>
</div>
</section>
<section id="change-log" class="tech-section">
<h1>Change Log</h1>
<p>2012.12.30: Updated introduction with improved wording. Working on CDN section. Added very basic database content and removed stub from that section.</p>
<p>2012.12.27: Added beginning of section on Python web frameworks. Added stub for content delivery network.</p>
<p>2012.12.25: Initial incomplete release on fullstackpython.com.</p>
</section>
</div>
</div>
<hr/>
<div class="footer pull-right">
<a href="http://www.mattmakai.com/" class="underline">Matt Makai</a> 2013
</div>
</div>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="/js/bootstrap.min.js"></script>
</body>
</html>