天天看點

windows下使用webpy搭建本地伺服器的曆程

首先是我剛開始對webpy什麼都不了解,後來通過一早上學習它,後來我成功搭建了webpy了。我主要是通過webpy官網提供的教程來的,具體可以看官網http://webpy.org/,這裡寫一些心得,其實webpy是一種簡潔的網絡應用架構,其部署還是要通過apache和nginx等Web伺服器軟體來實施的。下面是我的曆程:

這裡申明我的環境和成果:python2.7,使用mongodb,同時使用python官方提供中的pymongo提供的api。

1.安裝python,這裡我用的是python2.7,首先是下載下傳python安裝包,下載下傳完後輕按兩下安裝,安裝的時候記得自己的安裝python的安裝路徑,我的是安裝在D:\Python27中

windows下使用webpy搭建本地伺服器的曆程

2.然後配置環境變量,右鍵我的電腦,點選屬性,點選進階系統配置,然後點選環境變量,在系統變量那一欄裡面找到Path,然後輕按兩下。

windows下使用webpy搭建本地伺服器的曆程

3.然後就是添加你剛剛安裝python的路徑了,我的是D:\Python,添加的時候記得記得路勁後面帶;,不然的話會成功。

windows下使用webpy搭建本地伺服器的曆程

4.然後就打開cmd了,windows的終端,檢視python是安裝成功,出現下圖就安裝python成功了。

windows下使用webpy搭建本地伺服器的曆程

5.然後就是安裝python的包管理工具pip,這裡是具體安裝方法:http://www.360doc.com/content/14/1017/08/10724725_417590569.shtml,至于裡面的get-pip.py的連結打不開,可以到python官網下載下傳get-pip.py。

6.然後就是安裝pymongo在windows的終端下輸入pip install pymongo。

windows下使用webpy搭建本地伺服器的曆程

7.然後先安裝mongo。這裡是具體安裝方法:http://www.cnblogs.com/lecaf/archive/2013/08/23/mongodb.html

8.然後就可以開始我們的搭建本地伺服器的之旅了,首先是我根據webpy官網和網上的一些資料寫出自己伺服器端的代碼:

server.py:

#-*-encoding: utf-8-*-
import web
import json
import model

# http://localhost:1234/news?module=2&start=504&num=1
urls = (
    \'/users\', \'Users\',
    \'/query\', \'Query\',
    \'/add1\',    \'AddForOwner\',
    \'/add2\',    \'AddForDebate\'
)


class Users:
	def POST(self):
		datafrom = web.data()
		userid = ""
		loop = len(datafrom) - 1
		result = ""
		while loop > 0:
			if datafrom[loop] is \'=\':
				break
			userid += datafrom[loop]
			loop -= 1
		result_id = 0
		result = model.userid_get(userid)
		return result

class Query:
#form的類型是一個代表查詢的類型0:代表使用者,1:代表已回答,2:代表未回答,另一個是代表一些必要的資訊:和前一種有的關聯是:0:userid,1:0,代表沒有意義,2:0,代表沒有意義
	def POST(self):
		datafrom = web.data()
		count = 0
		loop = 0
		type1 = ""
		id1 = ""
		while loop < len(datafrom):
			if datafrom[loop] is \'=\' or datafrom[loop] is \'&\':
				count += 1
			if count is 1:
				type1 += datafrom[loop]
			if count is 3:
				id1 += datafrom[loop]
			loop += 1
		result = model.queryForUser(type1, id1)
		return result

class AddForOwner:
	def POST(self):
		datafrom = web.data()
		count = 0
		loop = 0
		userid = ""
		paixing = ""
		paicontent = ""
		zhanpucontent = ""
		while loop < len(datafrom):
			if datafrom[loop] is \'=\' or datafrom[loop] is \'&\':
				count += 1
				if datafrom[loop] is \'=\':
					loop += 1
			if count is 1:
				userid += datafrom[loop]
			if count is 3:
				paixing += datafrom[loop]
			if count is 5:
				paicontent += datafrom[loop]
			if count is 7:
				zhanpucontent += datafrom[loop]
			loop += 1

		
		model.addInOwner(userid, paixing, paicontent, zhanpucontent)

class AddForDebate:
	def POST(self):
		datafrom = web.data()
		count = 0
		loop = 0
		userid = ""
		resultid = ""
		content = ""
		while loop < len(datafrom):
			if datafrom[loop] is \'=\' or datafrom[loop] is \'&\':
				count += 1
				if datafrom[loop] is \'=\':
					loop += 1
			if count is 1:
				userid += datafrom[loop]
			if count is 3:
				resultid += datafrom[loop]
			if count is 5:
				content += datafrom[loop]
			loop += 1
		model.addInOther(userid, resultid, content)


app = web.application(urls, globals())

if __name__ == \'__main__\':
    app.run()      

model.py(資料庫操作的地方):

import web, datetime, time
import pymongo
import random
from pymongo import MongoClient


conn = pymongo.MongoClient(\'localhost\', 27017)

if conn:
	print "Successly Connect database!"

db = conn[\'Tiantainzhanpu\']

def userid_get(isRegistered):
	result = ""
	if isRegistered is "1":
		randnum =  random.randint(10000,10000000)
		total = db.user.find().count() + 1
		print total
		total = total + randnum
		db.user.insert_one({"userid": str(total)})
		result = str(total)
	return result


def queryForUser(type1, id1):
	if type1 is "0":
		userid = id1
		db.taolun.find({"userid": userid})
		return resultForUser
	elif type1 is "1":
		resultForUser = db.taolun.find({"status": "1"})
		return resultForUser
	else:
		resultForUser = db.taolun.find({"status": "0"})
		return resultForUser

def resultid_get(resultid):

	result = db.content.find({"resultid": resultid})
	return result

def addInOwner(userid, paixing, paicontent, zhanpucontent):
	numbers = db.taolun.find({}).count()
	numbers = numbers + 1
	db.taolun.insert_one({"resultid": str(numbers), "userid": userid, "type": paixing, "paicontent": paicontent, "zhanpucontent":zhanpucontent, "status": "0", "count": "0", "time":time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))})

def addInOther(userid, resultid, content):
	db.taolun.update_one({"resultid": resultid}, {\'$set\': {"status": "1"}})
	taoluncount = db.content.find({"resultid": resultid}).count()
	inctaoluncount = taoluncount + 1
	db.taolun.update({"resultid": resultid}, {\'$set\': {"count": str(inctaoluncount)}})
	db.content.insert_one({"resultid": resultid, "userid": userid, "content": content, "time":time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))})
      

  

 這裡講解幾點:1.安裝webpy子產品:這裡是安裝的具體方法:http://blog.chinaunix.net/uid-21633169-id-4239409.html,安裝完之後就可以import web了,

2.然後就是webpy中讀取的post當中的内容隻是一串字元串,還要我們自己解析的。這裡是它官網的介紹:http://webpy.org/cookbook/postbasic

3.之後的就沒有什麼問題了,可以觀看webpy提供的線上教程來就可以了。這裡附上網址:http://webpy.org/src/