cppcheck/tools/donate-cpu-server.py

73 lines
2.0 KiB
Python
Raw Normal View History

# 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
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')
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])
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:
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)
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))
f = open(resultPath + '/' + res.group(1), 'wt')
2018-08-24 13:04:25 +02:00
f.write(strDateTime() + '\n' + data[pos+1:])
f.close()
2018-08-23 22:13:53 +02:00
else:
2018-08-24 13:04:25 +02:00
print('[' + strDateTime() + '] invalid cmd')
finally:
connection.close()