Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Jacobs committed Apr 30, 2012
0 parents commit 3c70e9b
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions server.py
@@ -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()

0 comments on commit 3c70e9b

Please sign in to comment.