-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheg1.py
137 lines (134 loc) · 4.34 KB
/
eg1.py
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
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
from Request import Request
import Response
from Response import Response
# HTTPRequestHandler class
#global siteNames
class TestHTTPServerRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
print("************************************************")
mime=MimeTypes()
mimeType=mime.guess_type(self.path)
print("Request aayi")
urlParseData=urlparse(self.path)
print("URLPARSEDATA**** ",urlParseData)
urlPath=urlParseData[2]
query=urlParseData[4]
print("QUERY *****",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 if 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'
newPath=urlPath+'/index.html'
else:
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.htm'
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.py'
else:
self.send_error(404,"Homepage 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 Resource Not Found","utf-8"))
else:
a=self.path.find("/")
print("index ",a)
pathHavingResourceDetail=urlPath[urlPath.find("/",a+1):]
print("aaaaabcdcdbvava",pathHavingResourceDetail)
mapping=siteNames[path[1]].getMapping()
if pathHavingResourceDetail in mapping:
print("py file exists.")
a=mapping[pathHavingResourceDetail].rfind(".")
if a!=-1:
fileName=mapping[pathHavingResourceDetail][a+1:]
print(fileName)
packageName=mapping[pathHavingResourceDetail][:a]
print(packageName)
finalpath='apps.'+path[1]+".private."+packageName+"."+fileName
moduleName=__import__(finalpath,fromlist=fileName)
#request=Request
self.send_response(200)
request=Request(self,query)
response=Response(self,self.wfile,'text/html')
moduleName.process(request,response)
#self.send_header('Content-type',response.getContentType())
#self.end_headers()
return
else:
finalpath='apps.'+path[1]+".private."+mapping[pathHavingResourceDetail]
moduleName=__import__(finalpath,fromlist=mapping[pathHavingResourceDetail])
self.send_response(200)
request=Request(self,query)
response=Response(self,self.wfile,'text/html')
moduleName.process(request,response)
#self.send_header('Content-type',response.getContentType())
#self.end_headers()
return
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"], configuration["port"])
httpd = HTTPServer(server_address,TestHTTPServerRequestHandler)
print('running server...')
httpd.serve_forever()
run()