60 lines
1.5 KiB
Python
60 lines
1.5 KiB
Python
|
|
# Server for 'donate-cpu.py'
|
|
|
|
import os
|
|
import socket
|
|
import re
|
|
|
|
resultPath = os.path.expanduser('~/donated-results')
|
|
|
|
f = open('packages.txt', 'rt')
|
|
packages = f.readlines()
|
|
f.close()
|
|
|
|
print('packages:'+str(len(packages)))
|
|
|
|
if len(packages) == 0:
|
|
print('fatal: there are no packages')
|
|
sys.exit(1)
|
|
|
|
packageIndex = 0
|
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
server_address = ('', 8000)
|
|
sock.bind(server_address)
|
|
|
|
sock.listen(1)
|
|
|
|
while True:
|
|
# wait for a connection
|
|
print 'waiting for a connection'
|
|
connection, client_address = sock.accept()
|
|
|
|
try:
|
|
cmd = connection.recv(16)
|
|
if cmd=='get\n':
|
|
connection.sendall(packages[packageIndex].strip())
|
|
packageIndex += 1
|
|
if packageIndex >= len(packages):
|
|
packageIndex = 0
|
|
elif cmd.startswith('write\n'):
|
|
data = cmd[6:]
|
|
while True:
|
|
d = connection.recv(1024)
|
|
if d:
|
|
data += d
|
|
else:
|
|
break
|
|
pos = data.find('\n')
|
|
if data.startswith('ftp://') and pos > 10:
|
|
url = data[:pos]
|
|
res = re.match(r'ftp://.*pool/main/[^/]+/([^/]+)/[^/]*tar.gz',url)
|
|
if res and (url+'\n' in packages):
|
|
f = open(resultPath + '/' + res.group(1), 'wt')
|
|
f.write(data[pos+1:])
|
|
f.close()
|
|
|
|
finally:
|
|
connection.close()
|
|
|