-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat_client.py
More file actions
54 lines (40 loc) · 967 Bytes
/
Copy pathchat_client.py
File metadata and controls
54 lines (40 loc) · 967 Bytes
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
#!/usr/bin/python
from socket import *
import sys
from signal import *
import os
def do_child(s,addr,msg):
name = msg.split(' ')[1]
while True:
text = raw_input(">>")
if text == 'quit':
msg = 'Q ' + name
s.sendto(msg,addr)
os.kill(os.getppid(),SIGKILL)
exit()
else:
msg = 'B %s %s'%(name,text)
s.sendto(msg,addr)
return
def do_parent(s):
while True:
msg,addr = s.recvfrom(2048)
print msg
def main():
HOST = sys.argv[1]
PORT = int(sys.argv[2])
ADDR = (HOST,PORT)
s = socket(AF_INET,SOCK_DGRAM,0)
name = raw_input("please input your name>>")
msg = 'L %s '%name
s.sendto(msg,ADDR)
pid = os.fork()
if pid < 0:
print "fail to create process"
return
elif pid == 0:
do_child(s,ADDR,msg)
else:
do_parent(s)
if __name__ == "__main__":
main()