forked from makson96/Dynamics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamics_server.py
94 lines (81 loc) · 2.22 KB
/
dynamics_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python2
#-*- coding: utf-8 -*-
##This software (including its Debian packaging) is available to you under the terms of the GPL-3, see "/usr/share/common-licenses/GPL-3".
##Software is created and maintained by Laboratory of Biomolecular Systems Simulation at University of Gdansk.
##Contributors:
##- Tomasz Makarewicz (makson96@gmail.com)
import os, shutil, tarfile
import socket
os.chdir(os.getenv("HOME"))
project_name = 'project1'
dynamics_dir = os.getenv("HOME")+'/.dynamics/'
project_dir = dynamics_dir+project_name + '/'
##Clean "project1" temporary directory if present.
try:
shutil.rmtree(project_dir)
except:
pass
##Creating "project1" temporary directories
if os.path.isdir(project_dir) == False:
os.makedirs(project_dir)
##Receive data
# Create a socket object
s = socket.socket()
# Get local machine name
host = socket.gethostname()
# Reserve a port for your service.
port = 60000
s.connect((host, port))
s.send("Hello server!")
with open(dynamics_dir + 'project1.tar.bz2', 'wb') as f:
print 'file opened'
while True:
print('receiving data...')
data = s.recv(1024)
print('data=%s', (data))
if not data:
break
# write data to a file
f.write(data)
f.close()
print('Successfully get the file')
s.close()
print('connection closed')
##Perform calculations
tar = tarfile.open(dynamics_dir + 'project1.tar.bz2')
tar.extractall(project_dir)
tar.close()
os.remove(dynamics_dir + 'project1.tar.bz2')
#Dynamics
tar = tarfile.open(dynamics_dir + 'project2.tar.bz2', "w:bz2")
tar.add(project_dir, recursive=True, arcname="project2")
tar.close()
##Sends results back
# Reserve a port for your service.
port = 60000
# Create a socket object
s = socket.socket()
# Get local machine name
host = socket.gethostname()
# Bind to the port
s.bind((host, port))
# Now wait for client connection.
s.listen(5)
print 'Server listening....'
while True:
# Establish connection with client.
conn, addr = s.accept()
print 'Got connection from', addr
data = conn.recv(1024)
print('Server received', repr(data))
filename='mytext.txt'
f = open(filename,'rb')
l = f.read(1024)
while (l):
conn.send(l)
print('Sent ',repr(l))
l = f.read(1024)
f.close()
print('Done sending')
conn.send('Thank you for connecting')
conn.close()