-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
81 lines (75 loc) · 2.36 KB
/
Copy pathconfig.py
File metadata and controls
81 lines (75 loc) · 2.36 KB
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
'''
This is the file to read configuration files
Format of the server configuration file:
SERVER_PORT=SERVER_PORT_NUMBER
PATH=PATH_OF_SERVER_SHARED_DIRECTORY
Format of the client configuration file:
SERVER=SERVER_HOSTNAME/IP
SERVER_PORT=SERVER_PORT_NUMBER
CLIENT_PORT=CLIENT_PORT_NUMBER
DOWNLOAD=PATH_OF_CLIENT_DOWNLOAD_DIRECTORY
@author: hao
'''
class config:
#define header
server_port='SERVER_PORT'
path="PATH"
server="SERVER"
client_port="CLIENT_PORT"
download="DOWNLOAD"
serverConfig="server.config"
clientConfig="client.config"
def __init__(self):
pass
def readServerConfig(self):
try:
with open(self.serverConfig,'r') as f:
serPort=0
sharePath=""
for l in f:
sub=l.strip().split("=")
if(sub[0]==self.server_port):
serPort=int(sub[1])
elif(sub[0]==self.path):
sharePath=sub[1]
else:
pass
return serPort, sharePath
except:
print(Exception.message())
def readClientConfig(self):
'''
This function read client configuration file, return four values
@return: serverName
@return: serverPort
@return: clientPort
@return: downloadPath
'''
try:
with open(self.clientConfig,'r') as f:
serPort=0
serName=""
clientPort=0
downPath=""
for l in f:
sub=l.strip().split("=")
if(sub[0]==self.server_port):
serPort=int(sub[1])
elif(sub[0]==self.server):
serName=sub[1]
elif(sub[0]==self.client_port):
clientPort=sub[1]
elif(sub[0]==self.download):
downPath=sub[1]
else:
pass
return serName, serPort, clientPort, downPath
except:
print(Exception.message())
# The function to test the configuration class
def test():
conf=config()
client=conf.readClientConfig()
server=conf.readServerConfig()
print(client)
print(server)