基于Django的web多站点管理小系统

项目由来:每次部署网站都要改配置文件 so 自己写一个小应用来解决一下这个问题

实现功能:适用于lamp 或者 lnmp +ftp,在不登录shell的情况下配置web服务器。

思路:利用Python+Django+mysql修改配置文件和储存站点信息

以下不在赘述Django的配置。

业务代码如下:

# -*- coding:utf-8 -*-
from django.shortcuts import render
from django.http import HttpResponse,HttpResponseRedirect
from manage.models import Super,Nginx_Config,Ftp_Config,Database_Config,User
from django.contrib.auth.hashers import make_password, check_password
import time
import os
import shutil 
from django.conf import settings
from django.core import serializers
# from myfun import checklogin,gettime,conf_tem
from myfun import *
 
@checklogin
def index(request):
	data={}
	view = render(request,'super/index/index.html',data)
	return HttpResponse(view)
 
@checklogin
def nginxlist(request):
	data={}
	if(request.GET.has_key("id")):
		id=request.GET["id"]
		try:
			nginx_config=Nginx_Config.objects.get(pk=id)
			conf_file_path="/etc/nginx/conf.d/"+nginx_config.name+".conf"
			Nginx_Config.objects.filter(pk=id).delete()
			if(Nginx_Config.objects.filter(pk=id).count()==0):
				data["message"]="删除成功!"
				os.remove(conf_file_path)
				os.system('systemctl reload nginx.service')
			else:
				data["error"]="删除失败!"
		except Exception as e:
			data["error"]=e
 
 
	view = render(request,'super/sys/Nginx/index.html',data)
	return HttpResponse(view)
 
@checklogin
def nginxlistdata(request):
	array=[]
	nginxlist=Nginx_Config.objects.all()
	for x in nginxlist:
		x.status=http_status(get_status("http://"+x.domain))
		array.append(x)
	data = serializers.serialize("json", array)
	return HttpResponse(list(data))
 
@checklogin
def nginxadd(request):
	data={}
	if(request.method=='POST'):
		name		= request.POST['name']
		domain		= request.POST['domain']
		path		= request.POST['path']
		# config_cont	= request.POST['config_cont']
		config_cont	= conf_tem(domain,path)
		add_user	= request.session['s_name']
		created_at	= gettime()
		nginx_count=Nginx_Config.objects.filter(name=name).count()
		if(nginx_count==0):
			nc=Nginx_Config(name=name,domain=domain,path=path,created_at=created_at)
			nc.save()
			conf_file_name=name+".conf"
			write_file_obj=open("/etc/nginx/conf.d/"+conf_file_name,'w')
			write_file_obj.write(config_cont.encode('utf-8'))
			write_file_obj.close()
			added=Nginx_Config.objects.filter(name=name).count()
			if(added==1):
				document_root="/var/www/html/"+path
				isExists=os.path.exists(document_root)
				if not isExists:
					os.makedirs(document_root) 
				else:
					data["error"]="目录已存在!"
				shutil.copy("/var/www/templetes/index.html",document_root+"/index.html")
				data["message"]="添加成功!"
				os.system('systemctl reload nginx.service')
			else:
				data["error"]="添加失败!"
 
		else:
			data["error"]="存在同名站点!"
	view = render(request,'super/sys/Nginx/add.html',data)
	return HttpResponse(view)
 
@checklogin
def nginxedit(request):
	data={}
	id=request.GET["id"]
	if(request.method=='POST'):
		name		= request.POST['name']
		domain		= request.POST['domain']
		path		= request.POST['path']
		# config_cont	= request.POST['config_cont']
		config_cont	= conf_tem(domain,path)
		nginx_config=Nginx_Config.objects.get(pk=id)
		nginx_config.domain=domain
		nginx_config.path=path
		nginx_config.save()
 
		conf_file_name=name+".conf"
		write_file_obj=open("/etc/nginx/conf.d/"+conf_file_name,'w')
		write_file_obj.write(config_cont.encode('utf-8'))
		write_file_obj.close()
 
		data["message"]="修改成功!"
		os.system('systemctl reload nginx.service')
 
	nginx_info=Nginx_Config.objects.get(pk=id)
	if(os.path.exists("/etc/nginx/conf.d/"+nginx_info.name+".conf")):
		file_obj=open("/etc/nginx/conf.d/"+nginx_info.name+".conf")
		try:
			data["config_cont"]=file_obj.read()
		except Exception as e:
			data["e"]=e
		finally:
			file_obj.close()
 
	data["nginx_info"]=nginx_info
	view = render(request,'super/sys/Nginx/edit.html',data)
	return HttpResponse(view)
 
 
 
@checklogin
def ftplist(request):
	data={}
	if(request.GET.has_key("id")):
		id=request.GET["id"]
		try:
			ftp_config=Ftp_Config.objects.get(pk=id)
			Ftp_Config.objects.filter(pk=id).delete()
			if(Ftp_Config.objects.filter(pk=id).count()==0):
				data["message"]="删除成功!"
				#重写用户配置文件与更新用户数据库
				write_account_file()
				rmdir(ftp_config.path)
				os.system('systemctl reload nginx.service')
				#删除用户配置文件
				delete_ftp_user_file(ftp_config.name)
				os.system('systemctl reload nginx.service')
 
			else:
				data["error"]="删除失败!"
		except Exception as e:
			data["error"]=e
 
	view = render(request,'super/sys/Ftp/index.html',data)
	return HttpResponse(view)
 
@checklogin
def ftplistdata(request):
	array=[]
	ftplist=Ftp_Config.objects.all()
	data = serializers.serialize("json", ftplist)
	return HttpResponse(list(data))
 
@checklogin
def ftpadd(request):
	data={}
	if(request.method=='POST'):
		name		= request.POST['name']
		password	= request.POST['password']
		path		= request.POST['path']
		add_user	= request.session['s_name']
		created_at	= gettime()
 
		ftp_count=Ftp_Config.objects.filter(name=name).count()
		if(ftp_count==0):
 
			nc=Ftp_Config(name=name,password=password,path=path,created_at=created_at)
			nc.save()
 
			write_ftp_user_file(name)#写单用户配置文件
 
			added=Ftp_Config.objects.filter(name=name).count()
			if(added==1):
 
				ftp_dir="/var/www/html/"+path
				isExists=os.path.exists(ftp_dir)
				if not isExists:
					os.makedirs(ftp_dir) 
					shutil.copy("/var/www/templetes/index.html",ftp_dir+"/index.html")
					os.system('chown virtual_user:virtual_user '+ftp_dir+'/')
				else:
					data["error"]="目录已存在!"
 
				#写用户配置文件与更新用户数据库
				write_account_file()
				#重载配置文件
				os.system('systemctl reload vsftpd.service')
 
				data["message"]="添加成功!"
			else:
				data["error"]="添加失败!"
 
		else:
			data["error"]="存在同名FTP用户!"
	view = render(request,'super/sys/Ftp/add.html',data)
	return HttpResponse(view)
 
# @checklogin
# def nginxedit(request):
# 	data={}
# 	id=request.GET["id"]
# 	if(request.method=='POST'):
# 		name		= request.POST['name']
# 		domain		= request.POST['domain']
# 		path		= request.POST['path']
# 		# config_cont	= request.POST['config_cont']
# 		config_cont	= conf_tem(domain,path)
# 		nginx_config=Nginx_Config.objects.get(pk=id)
# 		nginx_config.domain=domain
# 		nginx_config.path=path
# 		nginx_config.save()
 
# 		conf_file_name=name+".conf"
# 		write_file_obj=open("/etc/nginx/conf.d/"+conf_file_name,'w')
# 		write_file_obj.write(config_cont.encode('utf-8'))
# 		write_file_obj.close()
 
# 		data["message"]="修改成功!"
# 		os.system('systemctl reload nginx.service')
 
# 	nginx_info=Nginx_Config.objects.get(pk=id)
# 	if(os.path.exists("/etc/nginx/conf.d/"+nginx_info.name+".conf")):
# 		file_obj=open("/etc/nginx/conf.d/"+nginx_info.name+".conf")
# 		try:
# 			data["config_cont"]=file_obj.read()
# 		except Exception as e:
# 			data["e"]=e
# 		finally:
# 			file_obj.close()
 
# 	data["nginx_info"]=nginx_info
# 	view = render(request,'super/sys/Nginx/edit.html',data)
# 	return HttpResponse(view)
 
 
@checklogin
def databaselist(request):
	data={}
	# if(request.GET.has_key("id")):
	# 	id=request.GET["id"]
	# 	try:
	# 		ftp_config=Database_Config.objects.get(pk=id)
	# 		Database_Config.objects.filter(pk=id).delete()
	# 		if(Database_Config.objects.filter(pk=id).count()==0):
	# 			data["message"]="删除成功!"
	# 			#重写用户配置文件与更新用户数据库
	# 			write_account_file()
	# 			rmdir(ftp_config.path)
	# 			os.system('systemctl reload nginx.service')
	# 			#删除用户配置文件
	# 			delete_ftp_user_file(ftp_config.name)
	# 			os.system('systemctl reload nginx.service')
 
	# 		else:
	# 			data["error"]="删除失败!"
	# 	except Exception as e:
	# 		data["error"]=e
 
	view = render(request,'super/sys/Database/index.html',data)
	return HttpResponse(view)
 
# @checklogin
# def databaselistdata(request):
# 	array=[]
# 	ftplist=User.objects.using('mysql').all()
# 	data = serializers.serialize("json", ftplist)
# 	return HttpResponse(list(data))
 
@checklogin
def databaselistdata(request):
	array=[]
	dblist=Database_Config.objects.all()
	data = serializers.serialize("json", dblist)
	return HttpResponse(list(data))
 
# CREATE USER 'lianghao11'@'localhost' IDENTIFIED BY 'zaq12wsx';
# GRANT USAGE ON *.* TO 'lianghao11'@'localhost' REQUIRE NONE WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;
# CREATE DATABASE IF NOT EXISTS `lianghao11`;
# GRANT ALL PRIVILEGES ON `lianghao11`.* TO 'lianghao11'@'localhost';
 
@checklogin
def databaseadd(request):
	data={}
	if(request.method=='POST'):
		name		= request.POST['name']
		password	= request.POST['password']
		databasename		= request.POST['databasename']
		created_at	= gettime()
		db_count=Database_Config.objects.filter(name=name).count()
		if(db_count==0):
			nc=Database_Config(name=name,password=password,databasename=databasename,created_at=created_at)
			nc.save()
			added=Ftp_Config.objects.filter(name=name).count()
			if(added==1):
				sql="CREATE USER '"+name+"'@'localhost' IDENTIFIED BY '"+password+"';GRANT USAGE ON *.* TO '"+name+"'@'localhost' REQUIRE NONE WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;CREATE DATABASE IF NOT EXISTS `"+databasename+"`;GRANT ALL PRIVILEGES ON `"+databasename+"`.* TO '"+name+"'@'localhost';"
				db_write(sql)
				data["message"]="添加成功!"
			else:
				data["error"]="添加失败!"
		else:
			data["error"]="存在同名数据库用户!"
 
	view = render(request,'super/sys/Database/add.html',data)
	return HttpResponse(view)

@checklogin
def index(request):
data={}
view = render(request,’super/index/index.html’,data)
return HttpResponse(view)

@checklogin
def nginxlist(request):
data={}
if(request.GET.has_key("id")):
id=request.GET["id"]
try:
nginx_config=Nginx_Config.objects.get(pk=id)
conf_file_path="/etc/nginx/conf.d/"+nginx_config.name+".conf"
Nginx_Config.objects.filter(pk=id).delete()
if(Nginx_Config.objects.filter(pk=id).count()==0):
data["message"]="删除成功!"
os.remove(conf_file_path)
os.system(‘systemctl reload nginx.service’)
else:
data["error"]="删除失败!"
except Exception as e:
data["error"]=e

view = render(request,’super/sys/Nginx/index.html’,data)
return HttpResponse(view)

@checklogin
def nginxlistdata(request):
array=[]
nginxlist=Nginx_Config.objects.all()
for x in nginxlist:
x.status=http_status(get_status("http://"+x.domain))
array.append(x)
data = serializers.serialize("json", array)
return HttpResponse(list(data))

@checklogin
def nginxadd(request):
data={}
if(request.method==’POST’):
name = request.POST[‘name’]
domain = request.POST[‘domain’]
path = request.POST[‘path’]
# config_cont = request.POST[‘config_cont’]
config_cont = conf_tem(domain,path)
add_user = request.session[‘s_name’]
created_at = gettime()
nginx_count=Nginx_Config.objects.filter(name=name).count()
if(nginx_count==0):
nc=Nginx_Config(name=name,domain=domain,path=path,created_at=created_at)
nc.save()
conf_file_name=name+".conf"
write_file_obj=open("/etc/nginx/conf.d/"+conf_file_name,’w’)
write_file_obj.write(config_cont.encode(‘utf-8’))
write_file_obj.close()
added=Nginx_Config.objects.filter(name=name).count()
if(added==1):
document_root="/var/www/html/"+path
isExists=os.path.exists(document_root)
if not isExists:
os.makedirs(document_root)
else:
data["error"]="目录已存在!"
shutil.copy("/var/www/templetes/index.html",document_root+"/index.html")
data["message"]="添加成功!"
os.system(‘systemctl reload nginx.service’)
else:
data["error"]="添加失败!"

else:
data["error"]="存在同名站点!"
view = render(request,’super/sys/Nginx/add.html’,data)
return HttpResponse(view)

@checklogin
def nginxedit(request):
data={}
id=request.GET["id"]
if(request.method==’POST’):
name = request.POST[‘name’]
domain = request.POST[‘domain’]
path = request.POST[‘path’]
# config_cont = request.POST[‘config_cont’]
config_cont = conf_tem(domain,path)
nginx_config=Nginx_Config.objects.get(pk=id)
nginx_config.domain=domain
nginx_config.path=path
nginx_config.save()

conf_file_name=name+".conf"
write_file_obj=open("/etc/nginx/conf.d/"+conf_file_name,’w’)
write_file_obj.write(config_cont.encode(‘utf-8’))
write_file_obj.close()

data["message"]="修改成功!"
os.system(‘systemctl reload nginx.service’)

nginx_info=Nginx_Config.objects.get(pk=id)
if(os.path.exists("/etc/nginx/conf.d/"+nginx_info.name+".conf")):
file_obj=open("/etc/nginx/conf.d/"+nginx_info.name+".conf")
try:
data["config_cont"]=file_obj.read()
except Exception as e:
data["e"]=e
finally:
file_obj.close()

data["nginx_info"]=nginx_info
view = render(request,’super/sys/Nginx/edit.html’,data)
return HttpResponse(view)

@checklogin
def ftplist(request):
data={}
if(request.GET.has_key("id")):
id=request.GET["id"]
try:
ftp_config=Ftp_Config.objects.get(pk=id)
Ftp_Config.objects.filter(pk=id).delete()
if(Ftp_Config.objects.filter(pk=id).count()==0):
data["message"]="删除成功!"
#重写用户配置文件与更新用户数据库
write_account_file()
rmdir(ftp_config.path)
os.system(‘systemctl reload nginx.service’)
#删除用户配置文件
delete_ftp_user_file(ftp_config.name)
os.system(‘systemctl reload nginx.service’)

else:
data["error"]="删除失败!"
except Exception as e:
data["error"]=e

view = render(request,’super/sys/Ftp/index.html’,data)
return HttpResponse(view)

@checklogin
def ftplistdata(request):
array=[]
ftplist=Ftp_Config.objects.all()
data = serializers.serialize("json", ftplist)
return HttpResponse(list(data))

@checklogin
def ftpadd(request):
data={}
if(request.method==’POST’):
name = request.POST[‘name’]
password = request.POST[‘password’]
path = request.POST[‘path’]
add_user = request.session[‘s_name’]
created_at = gettime()

ftp_count=Ftp_Config.objects.filter(name=name).count()
if(ftp_count==0):

nc=Ftp_Config(name=name,password=password,path=path,created_at=created_at)
nc.save()

write_ftp_user_file(name)#写单用户配置文件

added=Ftp_Config.objects.filter(name=name).count()
if(added==1):

ftp_dir="/var/www/html/"+path
isExists=os.path.exists(ftp_dir)
if not isExists:
os.makedirs(ftp_dir)
shutil.copy("/var/www/templetes/index.html",ftp_dir+"/index.html")
os.system(‘chown virtual_user:virtual_user ‘+ftp_dir+’/’)
else:
data["error"]="目录已存在!"

#写用户配置文件与更新用户数据库
write_account_file()
#重载配置文件
os.system(‘systemctl reload vsftpd.service’)

data["message"]="添加成功!"
else:
data["error"]="添加失败!"

else:
data["error"]="存在同名FTP用户!"
view = render(request,’super/sys/Ftp/add.html’,data)
return HttpResponse(view)

# @checklogin
# def nginxedit(request):
# data={}
# id=request.GET["id"]
# if(request.method==’POST’):
# name = request.POST[‘name’]
# domain = request.POST[‘domain’]
# path = request.POST[‘path’]
# # config_cont = request.POST[‘config_cont’]
# config_cont = conf_tem(domain,path)
# nginx_config=Nginx_Config.objects.get(pk=id)
# nginx_config.domain=domain
# nginx_config.path=path
# nginx_config.save()

# conf_file_name=name+".conf"
# write_file_obj=open("/etc/nginx/conf.d/"+conf_file_name,’w’)
# write_file_obj.write(config_cont.encode(‘utf-8’))
# write_file_obj.close()

# data["message"]="修改成功!"
# os.system(‘systemctl reload nginx.service’)

# nginx_info=Nginx_Config.objects.get(pk=id)
# if(os.path.exists("/etc/nginx/conf.d/"+nginx_info.name+".conf")):
# file_obj=open("/etc/nginx/conf.d/"+nginx_info.name+".conf")
# try:
# data["config_cont"]=file_obj.read()
# except Exception as e:
# data["e"]=e
# finally:
# file_obj.close()

# data["nginx_info"]=nginx_info
# view = render(request,’super/sys/Nginx/edit.html’,data)
# return HttpResponse(view)

@checklogin
def databaselist(request):
data={}
# if(request.GET.has_key("id")):
# id=request.GET["id"]
# try:
# ftp_config=Database_Config.objects.get(pk=id)
# Database_Config.objects.filter(pk=id).delete()
# if(Database_Config.objects.filter(pk=id).count()==0):
# data["message"]="删除成功!"
# #重写用户配置文件与更新用户数据库
# write_account_file()
# rmdir(ftp_config.path)
# os.system(‘systemctl reload nginx.service’)
# #删除用户配置文件
# delete_ftp_user_file(ftp_config.name)
# os.system(‘systemctl reload nginx.service’)

# else:
# data["error"]="删除失败!"
# except Exception as e:
# data["error"]=e

view = render(request,’super/sys/Database/index.html’,data)
return HttpResponse(view)

# @checklogin
# def databaselistdata(request):
# array=[]
# ftplist=User.objects.using(‘mysql’).all()
# data = serializers.serialize("json", ftplist)
# return HttpResponse(list(data))

@checklogin
def databaselistdata(request):
array=[]
dblist=Database_Config.objects.all()
data = serializers.serialize("json", dblist)
return HttpResponse(list(data))

# CREATE USER ‘lianghao11’@’localhost’ IDENTIFIED BY ‘zaq12wsx’;
# GRANT USAGE ON *.* TO ‘lianghao11’@’localhost’ REQUIRE NONE WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;
# CREATE DATABASE IF NOT EXISTS `lianghao11`;
# GRANT ALL PRIVILEGES ON `lianghao11`.* TO ‘lianghao11’@’localhost’;

@checklogin
def databaseadd(request):
data={}
if(request.method==’POST’):
name = request.POST[‘name’]
password = request.POST[‘password’]
databasename = request.POST[‘databasename’]
created_at = gettime()
db_count=Database_Config.objects.filter(name=name).count()
if(db_count==0):
nc=Database_Config(name=name,password=password,databasename=databasename,created_at=created_at)
nc.save()
added=Ftp_Config.objects.filter(name=name).count()
if(added==1):
sql="CREATE USER ‘"+name+"’@’localhost’ IDENTIFIED BY ‘"+password+"’;GRANT USAGE ON *.* TO ‘"+name+"’@’localhost’ REQUIRE NONE WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;CREATE DATABASE IF NOT EXISTS `"+databasename+"`;GRANT ALL PRIVILEGES ON `"+databasename+"`.* TO ‘"+name+"’@’localhost’;"
db_write(sql)
data["message"]="添加成功!"
else:
data["error"]="添加失败!"
else:
data["error"]="存在同名数据库用户!"

view = render(request,’super/sys/Database/add.html’,data)
return HttpResponse(view)

myfun文件如下:

# -*- coding:utf-8 -*-
from django.http import HttpResponse,HttpResponseRedirect
from manage.models import Super,Nginx_Config,Ftp_Config
import datetime
import requests
import time
import os
import shutil 
 
def checklogin(f):
	def wrapped_f(request):
		try:
			if(request.session["s_name"] == ""):
				return HttpResponseRedirect("/")
		except Exception as e:
			return HttpResponseRedirect("/")
		return f(request)
	return wrapped_f
 
def gettime():
	return datetime.datetime.now().strftime("%Y-%m-%d %H:%I:%S")
 
def conf_tem(domain='www',folder='html'):
	conf_cont="server {\n"
	conf_cont+="    listen       80;\n"
	conf_cont+="    server_name  "+domain+";\n"
	conf_cont+="    root   /var/www/html/"+folder+"/;\n"
	conf_cont+="    index index.php index.html index.htm;\n"
	conf_cont+="    location / {\n"
	conf_cont+="        try_files $uri $uri/ =404;\n"
	conf_cont+="        if (!-e  $request_filename) {\n"
	conf_cont+="            rewrite ^/(.*)$ /index.php?s=$1 last;\n"
	conf_cont+="        }\n"
	conf_cont+="    }\n"
	conf_cont+="    error_page 404 /404.html;\n"
	conf_cont+="    error_page 500 502 503 504 /50x.html;\n"
	conf_cont+="    location = /50x.html {\n"
	conf_cont+="        root /usr/share/nginx/html;\n"
	conf_cont+="    }\n"
	conf_cont+="    location ~ \.php$ {\n"
	conf_cont+="        try_files $uri =404;\n"
	conf_cont+="        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;\n"
	conf_cont+="        fastcgi_index index.php;\n"
	conf_cont+="        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;\n"
	conf_cont+="        include fastcgi_params;\n"
	conf_cont+="    }\n"
	conf_cont+="}\n"
	return conf_cont;
def get_status(url):  
	r = requests.get(url, allow_redirects = False)  
	return r.status_code
def prn_obj(obj):
	print '\n'.join(['%s:%s' % item for item in obj.__dict__.items()])
def http_status(code):
	status={
		100:"100-继续",
		101:"101-切换协议",
		200:"200-成功",
		201:"201-已创建",
		202:"202-已接受",
		203:"203-非授权信息",
		204:"204-无内容",
		205:"205-重置内容",
		206:"206-部分内容",
		300:"300-多种选择",
		301:"301-永久移动",
		302:"302-临时移动",
		303:"303-查看其他位置",
		304:"304-未修改",
		305:"305-使用代理",
		307:"307-临时重定向",
		400:"400-错误请求",
		401:"401-未授权",
		403:"403-禁止",
		404:"404-未找到",
		405:"405-方法禁用",
		406:"406-不接受",
		407:"407-需要代理授权",
		408:"408-请求超时",
		409:"409-冲突",
		410:"410-已删除",
		411:"411-需要有效长度",
		412:"412-未满足前提条件",
		413:"413-请求实体过大",
		414:"414-请求的URI 过长",
		415:"415-不支持的媒体类型",
		416:"416-请求范围不符合要求",
		417:"417-未满足期望值",
		500:"500-服务器内部错误",
		501:"501-尚未实施",
		502:"502-错误网关",
		503:"503-服务不可用",
		504:"504-网关超时",
		505:"505-HTTP版本不受支持"
	}
	return status[code]
 
def write_ftp_user_file(file_name):
	ftp_user_cont="local_root=/var/www/html/"+file_name+"\n"
	ftp_user_cont+="write_enable=YES\n"
 
	write_file_obj=open("/etc/vsftpd/vsftpd_user_conf/"+file_name,'w')
	write_file_obj.write(ftp_user_cont.encode('utf-8'))
	write_file_obj.close()
 
def delete_ftp_user_file(file_name):
	file="/etc/vsftpd/vsftpd_user_conf/"+file_name
	os.remove(file)
 
def write_account_file():
	os.remove("/etc/vsftpd/account.db")
	ftp_conf_cont=""
	ftp_user_list=Ftp_Config.objects.all()
	for ftp_user in ftp_user_list:
		ftp_conf_cont+=ftp_user.name+"\n"
		ftp_conf_cont+=ftp_user.password+"\n"
	write_account_cont=open("/etc/vsftpd/account.txt",'w')
	write_account_cont.write(ftp_conf_cont.encode('utf-8'))
	write_account_cont.close()
	os.system('db_load -T -t hash -f /etc/vsftpd/account.txt /etc/vsftpd/account.db')
 
def rmdir(dirname):
	os.system('rm -rf /var/www/html/'+dirname+'/')
 
def db_write(sql):
	from django.db import connection, transaction
	cursor = connection.cursor()
	# 数据修改操作——提交要求
	cursor.execute(sql)
	# transaction.commit_unless_managed()
 
def db_read(sql):
	# 数据检索操作,不需要提交
	cursor.execute(sql)
	row = cursor.fetchone()
 
	return row

def checklogin(f):
def wrapped_f(request):
try:
if(request.session["s_name"] == ""):
return HttpResponseRedirect("/")
except Exception as e:
return HttpResponseRedirect("/")
return f(request)
return wrapped_f

def gettime():
return datetime.datetime.now().strftime("%Y-%m-%d %H:%I:%S")

def conf_tem(domain=’www’,folder=’html’):
conf_cont="server {\n"
conf_cont+=" listen 80;\n"
conf_cont+=" server_name "+domain+";\n"
conf_cont+=" root /var/www/html/"+folder+"/;\n"
conf_cont+=" index index.php index.html index.htm;\n"
conf_cont+=" location / {\n"
conf_cont+=" try_files $uri $uri/ =404;\n"
conf_cont+=" if (!-e $request_filename) {\n"
conf_cont+=" rewrite ^/(.*)$ /index.php?s=$1 last;\n"
conf_cont+=" }\n"
conf_cont+=" }\n"
conf_cont+=" error_page 404 /404.html;\n"
conf_cont+=" error_page 500 502 503 504 /50x.html;\n"
conf_cont+=" location = /50x.html {\n"
conf_cont+=" root /usr/share/nginx/html;\n"
conf_cont+=" }\n"
conf_cont+=" location ~ \.php$ {\n"
conf_cont+=" try_files $uri =404;\n"
conf_cont+=" fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;\n"
conf_cont+=" fastcgi_index index.php;\n"
conf_cont+=" fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;\n"
conf_cont+=" include fastcgi_params;\n"
conf_cont+=" }\n"
conf_cont+="}\n"
return conf_cont;
def get_status(url):
r = requests.get(url, allow_redirects = False)
return r.status_code
def prn_obj(obj):
print ‘\n’.join([‘%s:%s’ % item for item in obj.__dict__.items()])
def http_status(code):
status={
100:"100-继续",
101:"101-切换协议",
200:"200-成功",
201:"201-已创建",
202:"202-已接受",
203:"203-非授权信息",
204:"204-无内容",
205:"205-重置内容",
206:"206-部分内容",
300:"300-多种选择",
301:"301-永久移动",
302:"302-临时移动",
303:"303-查看其他位置",
304:"304-未修改",
305:"305-使用代理",
307:"307-临时重定向",
400:"400-错误请求",
401:"401-未授权",
403:"403-禁止",
404:"404-未找到",
405:"405-方法禁用",
406:"406-不接受",
407:"407-需要代理授权",
408:"408-请求超时",
409:"409-冲突",
410:"410-已删除",
411:"411-需要有效长度",
412:"412-未满足前提条件",
413:"413-请求实体过大",
414:"414-请求的URI 过长",
415:"415-不支持的媒体类型",
416:"416-请求范围不符合要求",
417:"417-未满足期望值",
500:"500-服务器内部错误",
501:"501-尚未实施",
502:"502-错误网关",
503:"503-服务不可用",
504:"504-网关超时",
505:"505-HTTP版本不受支持"
}
return status[code]

def write_ftp_user_file(file_name):
ftp_user_cont="local_root=/var/www/html/"+file_name+"\n"
ftp_user_cont+="write_enable=YES\n"

write_file_obj=open("/etc/vsftpd/vsftpd_user_conf/"+file_name,’w’)
write_file_obj.write(ftp_user_cont.encode(‘utf-8’))
write_file_obj.close()

def delete_ftp_user_file(file_name):
file="/etc/vsftpd/vsftpd_user_conf/"+file_name
os.remove(file)

def write_account_file():
os.remove("/etc/vsftpd/account.db")
ftp_conf_cont=""
ftp_user_list=Ftp_Config.objects.all()
for ftp_user in ftp_user_list:
ftp_conf_cont+=ftp_user.name+"\n"
ftp_conf_cont+=ftp_user.password+"\n"
write_account_cont=open("/etc/vsftpd/account.txt",’w’)
write_account_cont.write(ftp_conf_cont.encode(‘utf-8’))
write_account_cont.close()
os.system(‘db_load -T -t hash -f /etc/vsftpd/account.txt /etc/vsftpd/account.db’)

def rmdir(dirname):
os.system(‘rm -rf /var/www/html/’+dirname+’/’)

def db_write(sql):
from django.db import connection, transaction
cursor = connection.cursor()
# 数据修改操作——提交要求
cursor.execute(sql)
# transaction.commit_unless_managed()

def db_read(sql):
# 数据检索操作,不需要提交
cursor.execute(sql)
row = cursor.fetchone()

return row

点赞

发表评论

邮箱地址不会被公开。 必填项已用*标注