def do_GET(self): root_dir = './Proxy/' try: #Check the file extension required and set the right mime type if self.path.endswith(".html"): mime_type = 'text/html' send_reply = True elif self.path.endswith(".txt"): mime_type = 'text/plain' send_reply = True elif self.path.endswith(".jpg"): mime_type = 'image/jpg' send_reply = True elif self.path.endswith(".gif"): mime_type = 'image/gif' send_reply = True elif self.path.endswith(".js"): mime_type = 'application/javascript' send_reply = True elif self.path.endswith(".css"): mime_type = 'text/css' send_reply = True elif self.path.endswith(".pdf"): mime_type = 'application/pdf' send_reply = True else: mime_type = 'text/plain' send_reply = False if send_reply: #Open the static file requested and send it #f = open(curdir + sep + self.path, 'rb') f = open(root_dir + self.path, 'rb') self.send_response(200) self.send_header('Content-type', mime_type) self.end_headers() self.wfile.write(f.read()) f.close() return except IOError: #Request file from origin server and wait for response sock = MyTCP.create_socket(UDP_IP) sock.sendto(self.path, (UDP_IP, UDP_PORT)) data, addr = sock.recvfrom(1024) f = open(root_dir + self.path, 'wb') f.write(data) f.close() print 'file received' self.send_error(404, 'File Not Found: %s' % self.path)
__author__ = 'Kyle' import MyTCP UDP_IP = '127.0.0.1' UDP_PORT = 5005 root_dir = './Origin/' try: sock = MyTCP.create_socket(UDP_IP, UDP_PORT) while True: data, addr = sock.recvfrom(1024) print 'received message: ', data print 'Sending file', data, '...' try: f = open(root_dir + data, 'rb') data_out = f.read() sock.sendto(data_out, addr) f.close() except IOError: print "oops" except KeyboardInterrupt: print '^C received, shutting down the web server' sock.socket.close()
def request(self, file_name): self.file_name = file_name self.myTCP_socket = MyTCP.MyTCPSocket() self.myTCP_socket.connect(UDP_IP, UDP_PORT) self.myTCP_socket.send(self.file_name)