Created
October 30, 2012 19:47
-
-
Save ik5/3982555 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer | |
from os import curdir, sep, stat | |
PORT_NUMBER = 8080 | |
#This class will handles any incoming request from | |
#the browser | |
class myHandler(BaseHTTPRequestHandler): | |
#Handler for the GET requests | |
def do_GET(self): | |
if self.path=="/": | |
self.path="/file.7z" | |
print "path: ", self.path | |
try: | |
#Check the file extension required and | |
#set the right mime type | |
sendReply = False | |
if self.path.endswith(".7z"): | |
print "We got 7z request" | |
mimetype='application/octet-stream' | |
sendReply=True | |
if sendReply == True: | |
full_file = curdir + sep + self.path | |
print "Going to send data from: ", full_file | |
#Open the static file requested and send it | |
f = open(full_file) | |
self.send_response(200) | |
self.send_header('Content-type',mimetype) | |
self.send_header('Content-Disposition: inline; filename="', 'Doctor.Who.2005.Season.4.DVDrip.x264.Dolby.5.1-MCH.7z"') | |
self.send_header('Content-length: ', stat(full_file).st_size) | |
self.send_header('Cache-Control:', 'no-cache, must-revalidate') | |
self.send_header('Pragma:', 'no-cache') | |
self.end_headers() | |
self.wfile.write(f.read()) | |
f.close() | |
print "Done sending data. Bye" | |
return | |
except IOError: | |
self.send_error(404,'File Not Found: %s' % self.path) | |
try: | |
#Create a web server and define the handler to manage the | |
#incoming request | |
server = HTTPServer(('', PORT_NUMBER), myHandler) | |
print 'Started httpserver on port ' , PORT_NUMBER | |
#Wait forever for incoming htto requests | |
server.serve_forever() | |
except KeyboardInterrupt: | |
print '^C received, shutting down the web server' | |
server.socket.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment