-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkunal.py
More file actions
145 lines (141 loc) · 4.53 KB
/
kunal.py
File metadata and controls
145 lines (141 loc) · 4.53 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
import socket
import sys
import os
import subprocess
from subprocess import Popen,PIPE
from http.server import BaseHTTPRequestHandler, HTTPServer
from mimetypes import MimeTypes
import tmserver
from pprint import pprint
from urllib.parse import *
import importlib
import Request,Response
# HTTPRequestHandler class
#global siteNames
import http.cookies as Cookie
class TestHTTPServerRequestHandler(BaseHTTPRequestHandler):
def sendCookie(self):
c=Cookie.SimpleCookie()
c['name']='Bablu'
c['sex']='M'
self.send_header('Set-Cookie',c.output(header=''))
self.end_headers()
self.wfile.write(bytes("<!doctype html><html><head><meta charset='utf-8'></head><body><a href='/kunal'>Cookie</a>"))
def do_GET(self):
print("************************************************")
if self.path=='/kunal':
if "Cookie" in self.headers:
c = Cookie.SimpleCookie(self.headers["Cookie"])
print(c['name'].value)
c=Cookie.SimpleCookie()
c['name']='Bablu'
c['sex']='M'
self.send_response(200)
self.send_header('Set-Cookie',c.output(header=''))
self.send_header('Content-type','text/html')
self.end_headers()
self.wfile.write(bytes("<!doctype html><html lang='en'><head><meta charset='utf-8'></head><body><a href='/kunal'>Cool</a>",'utf-8'))
return
mime=MimeTypes()
mimeType=mime.guess_type(self.path)
print("Request aayi")
urlParseData=urlparse(self.path)
urlPath=urlParseData[2]
query=urlParseData[4]
print(query)
if query!='':
query=parse_qs(query)
print(query)
print("name ",query["nm"][0])
newPath=''
if urlPath=='/' or urlPath=='/favicon.ico':
newPath='./default/index.html'
print("default wale iff me agya")
else:
path=urlPath
path=path.split("/")
print(path[1])
if path[1] in siteNames :
if len(path)==2:
print('./apps/'+path[1]+'/index.html')
if os.path.isfile('./apps/'+path[1]+'/index.html'):
print('index.html found')
# urlPath='./apps/'+path[1]+'/index.html'
print('index.html not found')
if os.path.isfile('./apps/'+path[1]+'/index.htm'):
print('index.htm found')
#urlPath='./apps/'+path[1]+'/index.htm'
newPath=urlPath+'/index.html'
else:
print('index.htm not found')
if os.path.isfile('./apps/'+path[1]+'/index.py'):
print("index.py found")
# urlPath='./apps/'+path[1]+'/index.py'
newPath=urlPath+'/index.html'
else:
self.send_error(404,"home page not found")
return
self.send_response(301)
self.send_header('Location',newPath)
self.end_headers()
return
else:
if path[2]=='private':
print("Kuch nahi ho sakta")
self.wfile.write(bytes("404 NOT FOUND","utf-8"))
else:
a=self.path.find("/")
print("index ",a)
abcd=urlPath[urlPath.find("/",a+1):]
print("aaaaabcdcdbvava",abcd)
mapping=siteNames[path[1]].getMapping()
if abcd in mapping:
print("py exitssssssssssssssssss karti h")
a=mapping[abcd].rfind(".")
if a!=-1:
fileName=mapping[abcd][a+1:]
print(fileName)
packageName=mapping[abcd][:a]
ieiprint(packageName)
finalpath='apps.'+path[1]+".private."+packageName+"."+fileName
moduleName=__import__(finalpath,fromlist=fileName)
request=Request(query,self.command)
response=Response(self.wfile,'text/html')
moduleName.process(request,response)
else:
finalpath='apps.'+path[1]+".private."+mapping[abcd]
moduleName=__import__(finalpath,fromlist=mapping[abcd])
request=Request(query,self.command)
response=Response(self.wfile,'text/html')
moduleName.process()
else:
newPath='./apps'+urlPath#self.path[self.path.find("/"):]
mimeType=mime.guess_type(newPath)
else:
self.send_error(404,'%s Not Found' %(path[1]))
try:
#mimeType=mime.guess_type(newPath)
print(mimeType)
print(newPath)
f=open(newPath,'rb')
self.send_response(200)
self.send_header('Content-type',mimeType)
self.end_headers()
self.wfile.write(f.read())
f.close()
except IOError:
print("File does not exist");
self.send_error(404,'File Not Found: %s' % urlPath)
# print(urlPath)
return
def run():
global siteNames
siteNames=tmserver.loadSites()
configuration=tmserver.loadConfiguration()
print('starting server...')
server_address = (configuration["ip"],5051 )
httpd = HTTPServer(server_address,TestHTTPServerRequestHandler)
print('running server...')
httpd.serve_forever()
print("*Fhsahfahf******FHDHF*DHFHDH*D*D*HDF*DFH*")
run()