Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Eric Jacobs
committed
Apr 30, 2012
0 parents
commit 3c70e9b
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#!/usr/bin/env python | ||
import os | ||
import sys | ||
import SocketServer | ||
import tarfile | ||
import uuid | ||
import time | ||
|
||
# Some globals: | ||
tmpDir = './tmp/' # MUST END IN / | ||
tmpFilePrefix = 'recvr-' | ||
targetDir = '/tmp/mnt/sda1/tiles/' # ALSO MUST END IN / | ||
|
||
class TCPUploadReceive(SocketServer.StreamRequestHandler): | ||
|
||
def handle(self): | ||
tempFile = tmpDir + tmpFilePrefix + uuid.uuid4().hex | ||
done = False | ||
f = open(tempFile, 'w') | ||
bufferSize = 4096 | ||
|
||
log(tempFile + " opened for writing.") | ||
log("Receiving file from {}...".format(self.client_address[0])) | ||
|
||
while not done: | ||
bufferData = self.request.recv(bufferSize).strip() | ||
if bufferData != "": | ||
f.write(bufferData) | ||
else: | ||
done = True | ||
f.close() | ||
|
||
unTarFile(tempFile, targetDir) | ||
rmFile(tempFile) | ||
|
||
self.wfile.write('{ "success": "true" }') | ||
|
||
def unTarFile(tarball, target): | ||
log("Extracting...") | ||
with tarfile.open(path, 'r|*') as tarball: | ||
tarball.extractall(target) | ||
|
||
def rmFile(path): | ||
log("Removing temp file...") | ||
os.unlink(path) | ||
|
||
def log(message): | ||
logTime = '%H:%M:%S%%%y-%m-%d - ' | ||
|
||
sys.stdout.write(time.strftime(logTime)) | ||
sys.stdout.write(message) | ||
sys.stdout.write('\n') | ||
sys.stdout.flush() | ||
|
||
|
||
if __name__ == "__main__": | ||
# Some environment fixing, before we begin: | ||
if not os.path.exists(tmpDir): | ||
os.makedirs(tmpDir) | ||
if not os.path.exists(targetDir): | ||
os.makedirs(targetDir) | ||
|
||
HOST, PORT = "localhost", 9999 | ||
|
||
# Create the server, binding to localhost on port 9999 | ||
server = SocketServer.TCPServer((HOST, PORT), TCPUploadReceive) | ||
log("Receiver now listening on port " + str(PORT)) | ||
server.serve_forever() |