2018-08-23 21:31:02 +02:00
|
|
|
|
|
|
|
# Server for 'donate-cpu.py'
|
|
|
|
|
|
|
|
import os
|
|
|
|
import socket
|
|
|
|
import re
|
2018-08-24 13:04:25 +02:00
|
|
|
import datetime
|
|
|
|
|
|
|
|
def strDateTime():
|
|
|
|
d = datetime.date.strftime(datetime.datetime.now().date(), '%Y-%m-%d')
|
|
|
|
t = datetime.time.strftime(datetime.datetime.now().time(), '%H:%M')
|
|
|
|
return d + ' ' + t
|
|
|
|
|
2018-08-23 21:31:02 +02:00
|
|
|
|
|
|
|
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
|
2018-08-24 13:04:25 +02:00
|
|
|
print('[' + strDateTime() + '] waiting for a connection')
|
2018-08-23 21:31:02 +02:00
|
|
|
connection, client_address = sock.accept()
|
|
|
|
|
|
|
|
try:
|
|
|
|
cmd = connection.recv(16)
|
|
|
|
if cmd=='get\n':
|
2018-08-23 22:13:53 +02:00
|
|
|
packages[packageIndex] = packages[packageIndex].strip()
|
2018-08-24 13:04:25 +02:00
|
|
|
print('[' + strDateTime() + '] get:' + packages[packageIndex])
|
2018-08-23 22:13:53 +02:00
|
|
|
connection.sendall(packages[packageIndex])
|
2018-08-23 21:31:02 +02:00
|
|
|
packageIndex += 1
|
|
|
|
if packageIndex >= len(packages):
|
|
|
|
packageIndex = 0
|
|
|
|
elif cmd.startswith('write\n'):
|
|
|
|
data = cmd[6:]
|
2018-08-23 22:13:53 +02:00
|
|
|
while len(data) < 1024 * 1024:
|
2018-08-23 21:31:02 +02:00
|
|
|
d = connection.recv(1024)
|
|
|
|
if d:
|
|
|
|
data += d
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
pos = data.find('\n')
|
|
|
|
if data.startswith('ftp://') and pos > 10:
|
|
|
|
url = data[:pos]
|
2018-08-24 13:04:25 +02:00
|
|
|
print('[' + strDateTime() + '] write:'+url)
|
2018-08-23 21:31:02 +02:00
|
|
|
res = re.match(r'ftp://.*pool/main/[^/]+/([^/]+)/[^/]*tar.gz',url)
|
2018-08-23 22:13:53 +02:00
|
|
|
if res and url in packages:
|
2018-08-23 21:54:46 +02:00
|
|
|
print('results added for package ' + res.group(1))
|
2018-08-23 21:31:02 +02:00
|
|
|
f = open(resultPath + '/' + res.group(1), 'wt')
|
2018-08-24 13:04:25 +02:00
|
|
|
f.write(strDateTime() + '\n' + data[pos+1:])
|
2018-08-23 21:31:02 +02:00
|
|
|
f.close()
|
2018-08-23 22:13:53 +02:00
|
|
|
else:
|
2018-08-24 13:04:25 +02:00
|
|
|
print('[' + strDateTime() + '] invalid cmd')
|
2018-08-23 21:31:02 +02:00
|
|
|
|
|
|
|
finally:
|
|
|
|
connection.close()
|
|
|
|
|