1
2 '''
3 Default included ICMP publishers.
4
5 @author: Michael Eddington
6 @author: Adam Cecchetti
7 @version: $Id: icmp.py 795 2008-03-25 05:34:00Z meddingt $
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 import socket, time, sys
41 from Peach.publisher import Publisher
42
43 -class Icmp(Publisher):
44 '''
45 A simple Icmp publisher.
46 '''
47
48 _host = None
49 _socket = None
50
51 - def __init__(self, host, timeout = 0.1):
52 '''
53 @type host: string
54 @param host: Remote host
55 @type timeout: number
56 @param timeout: How long to wait for reponse
57 '''
58 self._host = host
59 self._timeout = float(timeout)
60
62 '''
63 Close connection if open.
64 '''
65 self.close()
66
73
78
79 - def send(self, data):
80 '''
81 Send data via sendall.
82
83 @type data: string
84 @param data: Data to send
85 '''
86 self._socket.sendall(data)
87
89 '''
90 Receive upto 10000 bytes of data.
91
92 @rtype: string
93 @return: received data.
94 '''
95
96 if size != None:
97 return self._socket.recv(size)
98
99 else:
100
101 self._socket.setblocking(0)
102
103 timeout = self._timeout
104 beginTime = time.time()
105 ret = ''
106
107 try:
108 while True:
109 if len(ret) > 0 or time.time() - beginTime > timeout:
110 break
111
112 try:
113 ret += self._socket.recv(10000)
114 except socket.error, e:
115 if str(e).find('The socket operation could not complete without blocking') == -1:
116 raise
117
118 except socket.error, e:
119 print "Tcp::Receive(): Caught socket.error [%s]" % e
120
121 self._socket.setblocking(1)
122 return ret
123
124
125