Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../lib64/python2....
File: SimpleHTTPServer.py
"""Simple HTTP Server.
[0] Fix | Delete
[1] Fix | Delete
This module builds on BaseHTTPServer by implementing the standard GET
[2] Fix | Delete
and HEAD requests in a fairly straightforward manner.
[3] Fix | Delete
[4] Fix | Delete
"""
[5] Fix | Delete
[6] Fix | Delete
[7] Fix | Delete
__version__ = "0.6"
[8] Fix | Delete
[9] Fix | Delete
__all__ = ["SimpleHTTPRequestHandler"]
[10] Fix | Delete
[11] Fix | Delete
import os
[12] Fix | Delete
import posixpath
[13] Fix | Delete
import BaseHTTPServer
[14] Fix | Delete
import urllib
[15] Fix | Delete
import urlparse
[16] Fix | Delete
import cgi
[17] Fix | Delete
import sys
[18] Fix | Delete
import shutil
[19] Fix | Delete
import mimetypes
[20] Fix | Delete
try:
[21] Fix | Delete
from cStringIO import StringIO
[22] Fix | Delete
except ImportError:
[23] Fix | Delete
from StringIO import StringIO
[24] Fix | Delete
[25] Fix | Delete
[26] Fix | Delete
class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
[27] Fix | Delete
[28] Fix | Delete
"""Simple HTTP request handler with GET and HEAD commands.
[29] Fix | Delete
[30] Fix | Delete
This serves files from the current directory and any of its
[31] Fix | Delete
subdirectories. The MIME type for files is determined by
[32] Fix | Delete
calling the .guess_type() method.
[33] Fix | Delete
[34] Fix | Delete
The GET and HEAD requests are identical except that the HEAD
[35] Fix | Delete
request omits the actual contents of the file.
[36] Fix | Delete
[37] Fix | Delete
"""
[38] Fix | Delete
[39] Fix | Delete
server_version = "SimpleHTTP/" + __version__
[40] Fix | Delete
[41] Fix | Delete
def do_GET(self):
[42] Fix | Delete
"""Serve a GET request."""
[43] Fix | Delete
f = self.send_head()
[44] Fix | Delete
if f:
[45] Fix | Delete
try:
[46] Fix | Delete
self.copyfile(f, self.wfile)
[47] Fix | Delete
finally:
[48] Fix | Delete
f.close()
[49] Fix | Delete
[50] Fix | Delete
def do_HEAD(self):
[51] Fix | Delete
"""Serve a HEAD request."""
[52] Fix | Delete
f = self.send_head()
[53] Fix | Delete
if f:
[54] Fix | Delete
f.close()
[55] Fix | Delete
[56] Fix | Delete
def send_head(self):
[57] Fix | Delete
"""Common code for GET and HEAD commands.
[58] Fix | Delete
[59] Fix | Delete
This sends the response code and MIME headers.
[60] Fix | Delete
[61] Fix | Delete
Return value is either a file object (which has to be copied
[62] Fix | Delete
to the outputfile by the caller unless the command was HEAD,
[63] Fix | Delete
and must be closed by the caller under all circumstances), or
[64] Fix | Delete
None, in which case the caller has nothing further to do.
[65] Fix | Delete
[66] Fix | Delete
"""
[67] Fix | Delete
path = self.translate_path(self.path)
[68] Fix | Delete
f = None
[69] Fix | Delete
if os.path.isdir(path):
[70] Fix | Delete
parts = urlparse.urlsplit(self.path)
[71] Fix | Delete
if not parts.path.endswith('/'):
[72] Fix | Delete
# redirect browser - doing basically what apache does
[73] Fix | Delete
self.send_response(301)
[74] Fix | Delete
new_parts = (parts[0], parts[1], parts[2] + '/',
[75] Fix | Delete
parts[3], parts[4])
[76] Fix | Delete
new_url = urlparse.urlunsplit(new_parts)
[77] Fix | Delete
self.send_header("Location", new_url)
[78] Fix | Delete
self.end_headers()
[79] Fix | Delete
return None
[80] Fix | Delete
for index in "index.html", "index.htm":
[81] Fix | Delete
index = os.path.join(path, index)
[82] Fix | Delete
if os.path.exists(index):
[83] Fix | Delete
path = index
[84] Fix | Delete
break
[85] Fix | Delete
else:
[86] Fix | Delete
return self.list_directory(path)
[87] Fix | Delete
ctype = self.guess_type(path)
[88] Fix | Delete
try:
[89] Fix | Delete
# Always read in binary mode. Opening files in text mode may cause
[90] Fix | Delete
# newline translations, making the actual size of the content
[91] Fix | Delete
# transmitted *less* than the content-length!
[92] Fix | Delete
f = open(path, 'rb')
[93] Fix | Delete
except IOError:
[94] Fix | Delete
self.send_error(404, "File not found")
[95] Fix | Delete
return None
[96] Fix | Delete
try:
[97] Fix | Delete
self.send_response(200)
[98] Fix | Delete
self.send_header("Content-type", ctype)
[99] Fix | Delete
fs = os.fstat(f.fileno())
[100] Fix | Delete
self.send_header("Content-Length", str(fs[6]))
[101] Fix | Delete
self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
[102] Fix | Delete
self.end_headers()
[103] Fix | Delete
return f
[104] Fix | Delete
except:
[105] Fix | Delete
f.close()
[106] Fix | Delete
raise
[107] Fix | Delete
[108] Fix | Delete
def list_directory(self, path):
[109] Fix | Delete
"""Helper to produce a directory listing (absent index.html).
[110] Fix | Delete
[111] Fix | Delete
Return value is either a file object, or None (indicating an
[112] Fix | Delete
error). In either case, the headers are sent, making the
[113] Fix | Delete
interface the same as for send_head().
[114] Fix | Delete
[115] Fix | Delete
"""
[116] Fix | Delete
try:
[117] Fix | Delete
list = os.listdir(path)
[118] Fix | Delete
except os.error:
[119] Fix | Delete
self.send_error(404, "No permission to list directory")
[120] Fix | Delete
return None
[121] Fix | Delete
list.sort(key=lambda a: a.lower())
[122] Fix | Delete
f = StringIO()
[123] Fix | Delete
displaypath = cgi.escape(urllib.unquote(self.path))
[124] Fix | Delete
f.write('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">')
[125] Fix | Delete
f.write("<html>\n<title>Directory listing for %s</title>\n" % displaypath)
[126] Fix | Delete
f.write("<body>\n<h2>Directory listing for %s</h2>\n" % displaypath)
[127] Fix | Delete
f.write("<hr>\n<ul>\n")
[128] Fix | Delete
for name in list:
[129] Fix | Delete
fullname = os.path.join(path, name)
[130] Fix | Delete
displayname = linkname = name
[131] Fix | Delete
# Append / for directories or @ for symbolic links
[132] Fix | Delete
if os.path.isdir(fullname):
[133] Fix | Delete
displayname = name + "/"
[134] Fix | Delete
linkname = name + "/"
[135] Fix | Delete
if os.path.islink(fullname):
[136] Fix | Delete
displayname = name + "@"
[137] Fix | Delete
# Note: a link to a directory displays with @ and links with /
[138] Fix | Delete
f.write('<li><a href="%s">%s</a>\n'
[139] Fix | Delete
% (urllib.quote(linkname), cgi.escape(displayname)))
[140] Fix | Delete
f.write("</ul>\n<hr>\n</body>\n</html>\n")
[141] Fix | Delete
length = f.tell()
[142] Fix | Delete
f.seek(0)
[143] Fix | Delete
self.send_response(200)
[144] Fix | Delete
encoding = sys.getfilesystemencoding()
[145] Fix | Delete
self.send_header("Content-type", "text/html; charset=%s" % encoding)
[146] Fix | Delete
self.send_header("Content-Length", str(length))
[147] Fix | Delete
self.end_headers()
[148] Fix | Delete
return f
[149] Fix | Delete
[150] Fix | Delete
def translate_path(self, path):
[151] Fix | Delete
"""Translate a /-separated PATH to the local filename syntax.
[152] Fix | Delete
[153] Fix | Delete
Components that mean special things to the local file system
[154] Fix | Delete
(e.g. drive or directory names) are ignored. (XXX They should
[155] Fix | Delete
probably be diagnosed.)
[156] Fix | Delete
[157] Fix | Delete
"""
[158] Fix | Delete
# abandon query parameters
[159] Fix | Delete
path = path.split('?',1)[0]
[160] Fix | Delete
path = path.split('#',1)[0]
[161] Fix | Delete
# Don't forget explicit trailing slash when normalizing. Issue17324
[162] Fix | Delete
trailing_slash = path.rstrip().endswith('/')
[163] Fix | Delete
path = posixpath.normpath(urllib.unquote(path))
[164] Fix | Delete
words = path.split('/')
[165] Fix | Delete
words = filter(None, words)
[166] Fix | Delete
path = os.getcwd()
[167] Fix | Delete
for word in words:
[168] Fix | Delete
if os.path.dirname(word) or word in (os.curdir, os.pardir):
[169] Fix | Delete
# Ignore components that are not a simple file/directory name
[170] Fix | Delete
continue
[171] Fix | Delete
path = os.path.join(path, word)
[172] Fix | Delete
if trailing_slash:
[173] Fix | Delete
path += '/'
[174] Fix | Delete
return path
[175] Fix | Delete
[176] Fix | Delete
def copyfile(self, source, outputfile):
[177] Fix | Delete
"""Copy all data between two file objects.
[178] Fix | Delete
[179] Fix | Delete
The SOURCE argument is a file object open for reading
[180] Fix | Delete
(or anything with a read() method) and the DESTINATION
[181] Fix | Delete
argument is a file object open for writing (or
[182] Fix | Delete
anything with a write() method).
[183] Fix | Delete
[184] Fix | Delete
The only reason for overriding this would be to change
[185] Fix | Delete
the block size or perhaps to replace newlines by CRLF
[186] Fix | Delete
-- note however that this the default server uses this
[187] Fix | Delete
to copy binary data as well.
[188] Fix | Delete
[189] Fix | Delete
"""
[190] Fix | Delete
shutil.copyfileobj(source, outputfile)
[191] Fix | Delete
[192] Fix | Delete
def guess_type(self, path):
[193] Fix | Delete
"""Guess the type of a file.
[194] Fix | Delete
[195] Fix | Delete
Argument is a PATH (a filename).
[196] Fix | Delete
[197] Fix | Delete
Return value is a string of the form type/subtype,
[198] Fix | Delete
usable for a MIME Content-type header.
[199] Fix | Delete
[200] Fix | Delete
The default implementation looks the file's extension
[201] Fix | Delete
up in the table self.extensions_map, using application/octet-stream
[202] Fix | Delete
as a default; however it would be permissible (if
[203] Fix | Delete
slow) to look inside the data to make a better guess.
[204] Fix | Delete
[205] Fix | Delete
"""
[206] Fix | Delete
[207] Fix | Delete
base, ext = posixpath.splitext(path)
[208] Fix | Delete
if ext in self.extensions_map:
[209] Fix | Delete
return self.extensions_map[ext]
[210] Fix | Delete
ext = ext.lower()
[211] Fix | Delete
if ext in self.extensions_map:
[212] Fix | Delete
return self.extensions_map[ext]
[213] Fix | Delete
else:
[214] Fix | Delete
return self.extensions_map['']
[215] Fix | Delete
[216] Fix | Delete
if not mimetypes.inited:
[217] Fix | Delete
mimetypes.init() # try to read system mime.types
[218] Fix | Delete
extensions_map = mimetypes.types_map.copy()
[219] Fix | Delete
extensions_map.update({
[220] Fix | Delete
'': 'application/octet-stream', # Default
[221] Fix | Delete
'.py': 'text/plain',
[222] Fix | Delete
'.c': 'text/plain',
[223] Fix | Delete
'.h': 'text/plain',
[224] Fix | Delete
})
[225] Fix | Delete
[226] Fix | Delete
[227] Fix | Delete
def test(HandlerClass = SimpleHTTPRequestHandler,
[228] Fix | Delete
ServerClass = BaseHTTPServer.HTTPServer):
[229] Fix | Delete
BaseHTTPServer.test(HandlerClass, ServerClass)
[230] Fix | Delete
[231] Fix | Delete
[232] Fix | Delete
if __name__ == '__main__':
[233] Fix | Delete
test()
[234] Fix | Delete
[235] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function