关于服务器——安装配置django

django是一个基于python语言的服务器框架,能够帮助我们轻易开发一个web应用,下面记录一些简单的安装使用方法。

sudo pip install Django==1.5.1

创建一个新的web服务(mysite,可以进行路由路径配置,数据库配置等)

django-admin.py startproject mysite

创建一个新的web应用(books,具体代码实现)

manage.py start app books

类似MVC框架,django提供model,view等模块供我们使用(不过django的model更类似于数据库定义,view类似MVC中的controller,template才是MVC中的view)编辑代码如下

books/models.py

from django.db import models

# Create your models here.
class Publisher(models.Model):
    name = models.CharField(max_length=30)
    address = models.CharField(max_length=50)
    city = models.CharField(max_length=60)
    state_province = models.CharField(max_length=30)
    country = models.CharField(max_length=50)
    website = models.URLField()

    def __unicode__(self):
        return self.name

#class Author(models.Model):
#    first_name = models.CharField(max_length=30)
#    last_name = models.CharField(max_length=40)
#    email = models.EmailField()
#
#    def __unicode__(self):
#        return u'%s %s' % (self.first_name, self.last_name)

#class Book(models.Model):
#    title = models.CharField(max_length=100)
#    authors = models.ManyToManyField(Author)
#    publisher = models.ForeignKey(Publisher)
#    publication_date = models.DateField()
#
#    def __unicode__(self):
#        return self.title

 books/views.py

# Create your views here.
from django.shortcuts import render_to_response
from books.models import Publisher

def list_publishers(request):
        publishers = Publisher.objects.all()
        return render_to_response('list_publishers.html', {'publishers': publishers})

 templates/list_publishers.html

<table>
        {% for p in publishers %}
        <tr>
        <td>Id #{{ p.id }}</td>
        <td>Name #{{ p.name }}</td>
        </tr>
        {% endfor %}
</table>

自定义配置web,设置数据库连接等

mysite/settings.py

INSTALLED_APPS = (
    'books',
)

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': '/home/ciaos/django.sqlite3',                      # Or path to database file if using sqlite3.
        # The following settings are not used with sqlite3:
        'USER': '',
        'PASSWORD': '',
        'HOST': '',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
        'PORT': '',                      # Set to empty string for default.
    }
}

TEMPLATE_DIRS = (
    '/home/ciaos/mysite/templates',
)

mysite/urls.py(利用python强大的正则表达式配置路由)

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'mysite.views.home', name='home'),
    # url(r'^mysite/', include('mysite.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^publishers/',list_publishers)
)

python manager.py syncdb同步models到数据库

通过python manage.py shell简单操作查询数据库

运行数据库如下(如果要后台运行可以使用nohup)

python manage.py runserver

 用ab测试

ciaos@linux-53dr:~/mysite> sudo /usr/sbin/ab2 -c 10 -n 10000 http://127.0.0.1:8000/publishers/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:        WSGIServer/0.1
Server Hostname:        127.0.0.1
Server Port:            8000

Document Path:          /publishers/
Document Length:        69 bytes

Concurrency Level:      10
Time taken for tests:   49.311 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      2020000 bytes
HTML transferred:       690000 bytes
Requests per second:    202.80 [#/sec] (mean)
Time per request:       49.311 [ms] (mean)
Time per request:       4.931 [ms] (mean, across all concurrent requests)
Transfer rate:          40.00 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    2  38.6      0    1001
Processing:    22   47  82.8     36    2562
Waiting:        6   41  82.7     31    2553
Total:         22   49  91.3     36    2562

Percentage of the requests served within a certain time (ms)
  50%     36
  66%     39
  75%     41
  80%     42
  90%     52
  95%     63
  98%    100
  99%    428
 100%   2562 (longest request)

并发数据等没有参考意义,因为是在我笔记本的虚拟机上测试的,主要是与tornado服务器做对比。用django实现一个简易的web应用还是非常方便的。

猜你喜欢

转载自ciaos.iteye.com/blog/1839627