-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublisher.py
43 lines (38 loc) · 1.2 KB
/
publisher.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
from socket import *
import sys
import shlex
MAX_BUF = 2048
SERV_PORT = 50000
def main():
while True:
try:
textout = sys.stdin.readline().strip() #read text from standard input
publish = shlex.split(textout) #split text with shell lexical split
if(textout == 'exit'): #exit
break
elif(len(publish) == 4): #check number of argument
if(publish[0] == 'publish'): #check command
address = (publish[1], SERV_PORT)
try:
s.connect(address) #connect socket
s.send(textout.encode('utf-8')) #send textout
except:
print('Invalid IP address')
else:
print('Invalid command!')
else:
print('Invalid argument')
except KeyboardInterrupt:
print('')
print('exit')
break
s.close()
if __name__ == '__main__':
try:
s = socket(AF_INET, SOCK_STREAM) #create socket
main()
except KeyboardInterrupt:
try:
sys.exit(0)
except SystemExit:
os._exit(0)