Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
57af072e
Commit
57af072e
authored
May 09, 2000
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a simple directory listing function.
parent
18865de7
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
9 deletions
+38
-9
Lib/SimpleHTTPServer.py
Lib/SimpleHTTPServer.py
+38
-9
No files found.
Lib/SimpleHTTPServer.py
View file @
57af072e
...
...
@@ -6,7 +6,7 @@ and HEAD requests in a fairly straightforward manner.
"""
__version__
=
"0.
3
"
__version__
=
"0.
4
"
import
os
...
...
@@ -14,6 +14,8 @@ import string
import
posixpath
import
BaseHTTPServer
import
urllib
import
cgi
from
StringIO
import
StringIO
class
SimpleHTTPRequestHandler
(
BaseHTTPServer
.
BaseHTTPRequestHandler
):
...
...
@@ -58,18 +60,45 @@ class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
"""
path
=
self
.
translate_path
(
self
.
path
)
if
os
.
path
.
isdir
(
path
):
self
.
send_error
(
403
,
"Directory listing not supported"
)
return
None
try
:
f
=
open
(
path
,
'rb'
)
except
IOError
:
self
.
send_error
(
404
,
"File not found"
)
return
None
f
=
self
.
list_directory
(
path
)
if
f
is
None
:
return
None
ctype
=
"text/HTML"
else
:
try
:
f
=
open
(
path
,
'rb'
)
except
IOError
:
self
.
send_error
(
404
,
"File not found"
)
return
None
ctype
=
self
.
guess_type
(
path
)
self
.
send_response
(
200
)
self
.
send_header
(
"Content-type"
,
self
.
guess_type
(
path
)
)
self
.
send_header
(
"Content-type"
,
ctype
)
self
.
end_headers
()
return
f
def
list_directory
(
self
,
path
):
try
:
list
=
os
.
listdir
(
path
)
except
os
.
error
:
self
.
send_error
(
404
,
"No permission to list directory"
);
return
None
list
.
sort
(
lambda
a
,
b
:
cmp
(
a
.
lower
(),
b
.
lower
()))
f
=
StringIO
()
f
.
write
(
"<h2>Directory listing for %s</h2>
\
n
"
%
self
.
path
)
f
.
write
(
"<hr>
\
n
<ul>
\
n
"
)
for
name
in
list
:
fullname
=
os
.
path
.
join
(
path
,
name
)
displayname
=
name
=
cgi
.
escape
(
name
)
if
os
.
path
.
islink
(
fullname
):
displayname
=
name
+
"@"
elif
os
.
path
.
isdir
(
fullname
):
displayname
=
name
+
"/"
name
=
name
+
os
.
sep
f
.
write
(
'<li><a href="%s">%s</a>
\
n
'
%
(
name
,
displayname
))
f
.
write
(
"</ul>
\
n
<hr>
\
n
"
)
f
.
seek
(
0
)
return
f
def
translate_path
(
self
,
path
):
"""Translate a /-separated PATH to the local filename syntax.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment