Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pyfs
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
pyfs
Commits
3a3f4991
Commit
3a3f4991
authored
Aug 10, 2013
by
willmcgugan@gmail.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes an issue serving binary files
parent
c564dcf1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
26 deletions
+27
-26
fs/expose/http.py
+27
-26
No files found.
fs/expose/http.py
View file @
3a3f4991
...
...
@@ -17,15 +17,15 @@ def _datetime_to_epoch(d):
return
mktime
(
d
.
timetuple
())
class
FSHTTPRequestHandler
(
SimpleHTTPServer
.
SimpleHTTPRequestHandler
):
"""A hacked together version of SimpleHTTPRequestHandler"""
def
__init__
(
self
,
fs
,
request
,
client_address
,
server
):
self
.
_fs
=
fs
SimpleHTTPServer
.
SimpleHTTPRequestHandler
.
__init__
(
self
,
request
,
client_address
,
server
)
def
do_GET
(
self
):
"""Serve a GET request."""
"""Serve a GET request."""
f
=
None
try
:
f
=
self
.
send_head
()
...
...
@@ -33,10 +33,10 @@ class FSHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
try
:
self
.
copyfile
(
f
,
self
.
wfile
)
except
socket
.
error
:
pass
pass
finally
:
if
f
is
not
None
:
f
.
close
()
f
.
close
()
def
send_head
(
self
):
"""Common code for GET and HEAD commands.
...
...
@@ -65,15 +65,15 @@ class FSHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
break
else
:
return
self
.
list_directory
(
path
)
ctype
=
self
.
guess_type
(
path
)
ctype
=
self
.
guess_type
(
path
)
try
:
info
=
self
.
_fs
.
getinfo
(
path
)
f
=
self
.
_fs
.
open
(
path
,
'r'
)
f
=
self
.
_fs
.
open
(
path
,
'r
b
'
)
except
FSError
,
e
:
self
.
send_error
(
404
,
str
(
e
))
return
None
self
.
send_response
(
200
)
self
.
send_header
(
"Content-type"
,
ctype
)
self
.
send_response
(
200
)
self
.
send_header
(
"Content-type"
,
ctype
)
self
.
send_header
(
"Content-Length"
,
str
(
info
[
'size'
]))
if
'modified_time'
in
info
:
self
.
send_header
(
"Last-Modified"
,
self
.
date_time_string
(
_datetime_to_epoch
(
info
[
'modified_time'
])))
...
...
@@ -103,11 +103,11 @@ class FSHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
f
.
write
(
"<html>
\n
<title>Directory listing for
%
s</title>
\n
"
%
displaypath
)
f
.
write
(
"<body>
\n
<h2>Directory listing for
%
s</h2>
\n
"
%
displaypath
)
f
.
write
(
"<hr>
\n
<ul>
\n
"
)
parent
=
dirname
(
path
)
if
path
!=
parent
:
f
.
write
(
'<li><a href="
%
s">../</a></li>'
%
urllib
.
quote
(
parent
.
rstrip
(
'/'
)
+
'/'
))
for
path
in
paths
:
f
.
write
(
'<li><a href="
%
s">
%
s</a>
\n
'
%
(
urllib
.
quote
(
path
),
cgi
.
escape
(
path
)))
...
...
@@ -119,45 +119,45 @@ class FSHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
self
.
send_header
(
"Content-Length"
,
str
(
length
))
self
.
end_headers
()
return
f
def
translate_path
(
self
,
path
):
# abandon query parameters
path
=
path
.
split
(
'?'
,
1
)[
0
]
path
=
path
.
split
(
'#'
,
1
)[
0
]
path
=
posixpath
.
normpath
(
urllib
.
unquote
(
path
))
return
path
def
serve_fs
(
fs
,
address
=
''
,
port
=
8000
):
"""Serve an FS instance over http
:param fs: an FS object
:param address: IP address to serve on
:param port: port number
"""
def
Handler
(
request
,
client_address
,
server
):
return
FSHTTPRequestHandler
(
fs
,
request
,
client_address
,
server
)
#class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
# pass
httpd
=
SocketServer
.
TCPServer
((
address
,
port
),
Handler
,
bind_and_activate
=
False
)
# pass
httpd
=
SocketServer
.
TCPServer
((
address
,
port
),
Handler
,
bind_and_activate
=
False
)
#httpd = ThreadedTCPServer((address, port), Handler, bind_and_activate=False)
httpd
.
allow_reuse_address
=
True
httpd
.
server_bind
()
httpd
.
server_activate
()
server_thread
=
threading
.
Thread
(
target
=
httpd
.
serve_forever
)
server_thread
.
start
()
server_thread
=
threading
.
Thread
(
target
=
httpd
.
serve_forever
)
server_thread
.
start
()
try
:
while
True
:
time
.
sleep
(
0.1
)
except
(
KeyboardInterrupt
,
SystemExit
):
httpd
.
shutdown
()
if
__name__
==
"__main__"
:
from
fs.osfs
import
OSFS
serve_fs
(
OSFS
(
'~/'
))
\ No newline at end of file
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