-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2-6-server.py
More file actions
executable file
·33 lines (28 loc) · 841 Bytes
/
2-6-server.py
File metadata and controls
executable file
·33 lines (28 loc) · 841 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
#!/usr/bin/python
from socket import *
from time import ctime
class UDPSocketServer():
def __init__(self):
self.host = 'localhost'
self.port = 21567
self.buffsize = 1024
self.addr = (self.host, self.port)
self.s_udp_socket = socket(AF_INET, SOCK_DGRAM)
self.s_udp_socket.bind(self.addr)
self.daytime_port = getservbyname('daytime','udp')
print 'self.daytime_port = ', self.daytime_port
def run(self):
try:
while True:
print 'Waiting for data...'
data, addr = self.s_udp_socket.recvfrom(self.buffsize)
if not data:
break
print 'Received from addr',addr,data
self.s_udp_socket.sendto('[%s] %s' % (ctime(), data), addr)
self.s_udp_socket.close()
except KeyboardInterrupt, e:
print 'CTRL+C end the server'
self.s_udp_socket.close()
udp_socket = UDPSocketServer()
udp_socket.run()