Сравнение постов

Различия между постами #126294 (21.08.2019 16:28) и #137478 (03.07.2020 02:13).
1
#!/usr/bin/env python3
1
 
2
# -*- coding: UTF-8 -*-
3
import json
4
#from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
5
from http.server import BaseHTTPRequestHandler
6
import os
7
from utils.utils import get_list_overlays, load_config, write_config, sort_inatll_pkg
8
from package import search
9
from findfsdb import on_find 
10
11
#repl = '\n'
12
class Handler(BaseHTTPRequestHandler):
13
	def __int__(self):
14
		self.p_list =[]
15
16
	def r_403(self):
17
		self.send_response(403)
18
		self.end_headers()
19
		print(self.client_address)
20
	
21
	def r_404(self):
22
		self.send_response(404)
23
		self.end_headers()
24
		print(self.client_address)
25
26
	def do_GET(self):
27
		self.r_t =""
28
		if self.client_address[0] == '127.0.0.1' or self.client_address[0].startswith('10.0'):
29
			self.send_response(200)
30
			self.end_headers()
31
			if self.path =="/":
32
33
				with open('./views/index.html', 'tr') as f:
34
					self.r_t=f.read()
35
				print(self.client_address)
36
				
37
			elif self.path == '/main':
38
				f = open("./README.txt", 'r')
39
				self.r_t = str(f.read())
40
				f.close()
41
42
			elif self.path == '/ovelays':
43
				overlays = get_list_overlays()
44
				#print(ovls)
45
				if overlays == "":
46
					overlays ="Error"
47
48
				self.r_t=json.dumps({"repositories": overlays})
49
				
50
			elif self.path == "/favicon.ico":
51
				with open('./favicon.png', 'rb') as f:
52
					self.r_t = f.read()
53
					
54
			elif self.path == '/logo.png':
55
				pass
56
57
			elif self.path.startswith( "/static/"):
58
				self.r_static()
59
				self.send_response(200)
60
				#print(self.r_t)
61
62
			elif self.path == '/get_dump_list':
63
				with open('./pkgs.txt', 'r') as fn:
64
					data = fn.read()
65
					pkg_list = data.split("\n")
66
					#fn.close()
67
				#print(pkg_list)
68
				self.r_t =json.dumps({"dump_portage": pkg_list})
69
70
			elif self.path.startswith("/?st_app="):
71
				config = load_config()
72
				param = self.path.replace("/?st_app=", "") 
73
				list_param = param.split(',')
74
				print(list_param)
75
				for i in list_param:
76
					if i.startswith('port'):
77
						port = int(i.split('=')[1])	
78
					elif i.startswith('Lang'):
79
						Lang = i.split('=')[1]
80
					
81
				write_config(port, Lang)
82
				print(config)
83
				print(param)
84
85
			elif self.path.startswith('/?p='):
86
				param = self.path.replace('/?p=', '')
87
				pk_list = []
88
				search_result = {}
89
				#if len(param.split('/')) == 2:
90
				#	param = param.split('/')[1]
91
				p_list = on_find(param)
92
				#print(p_list)
93
				if len(p_list) == 0:
94
					print("Never Found")
95
					self.r_t = str(json.dumps({"Package_result": p_list}))
96
				else:
97
					for p in p_list:
98
						#print(p)
99
						if len(p.split("/")) == 2:
100
							pk_list.append(search(p.split("/")[1]))
101
						else:
102
							pk_list.append(search(p))
103
						#print(pk)
104
					search_result = {"Package_result": pk_list}
105
					self.r_t = str(json.dumps(search_result))
106
107
			elif self.path.startswith("/get_seiings_app"):
108
109
				self.r_t = str(json.dumps(load_config()))
110
111
			elif self.path == '/get_portage':
112
113
				self.r_t = str(sort_inatll_pkg())
114
115
			elif '.py?' in self.path:
116
				print("loading")
117
				self.path = "/static/app" + str(self.path.split('?')[0])
118
				print(self.path)
119
				self.r_static()
120
121
			else:
122
				self.send_response(404)
123
				
124
				self.end_headers()
125
				print(str(self.client_address[0]) +"\t" + str(404))
126
127
	        # Send the html message
128
			#self.wfile.write(bytes(self.r_t, "utf-8"))
129
			try:
130
				return self.wfile.write(self.r_t)
131
			except TypeError:
132
				#print("TypeError")
133
				return self.wfile.write(bytes(self.r_t, 'utf-8'))
134
		else:
135
			self.r_403()
136
137
	def r_static(self):
138
		if os.path.exists('./views/' + self.path):
139
			#self.send_response(200)
140
			#self.send_header('Content-type','text/css')
141
			#self.end_headers()
142
			with open('./views/' + self.path, 'tr') as f:
143
				self.r_t=f.read()
144
			
145
		else:
146
			self.send_response(404)