Package Peach :: Package Publishers :: Module raw
[hide private]

Source Code for Module Peach.Publishers.raw

  1   
  2  ''' 
  3  Default included Raw publishers. 
  4   
  5  @author: Adam Cecchetti 
  6  @version: $Id: raw.py 795 2008-03-25 05:34:00Z meddingt $ 
  7  ''' 
  8   
  9  # 
 10  # Copyright (c) 2007-2008 Michael Eddington 
 11  # 
 12  # Permission is hereby granted, free of charge, to any person obtaining a copy  
 13  # of this software and associated documentation files (the "Software"), to deal 
 14  # in the Software without restriction, including without limitation the rights  
 15  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
 16  # copies of the Software, and to permit persons to whom the Software is  
 17  # furnished to do so, subject to the following conditions: 
 18  # 
 19  # The above copyright notice and this permission notice shall be included in     
 20  # all copies or substantial portions of the Software. 
 21  # 
 22  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
 23  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
 24  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
 25  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 26  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 27  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 28  # SOFTWARE. 
 29  # 
 30   
 31  # Authors: 
 32  #   Adam Cecchetti (adam@cecchetti.com) 
 33  # $Id: raw.py 795 2008-03-25 05:34:00Z meddingt $ 
 34   
 35   
 36  import socket, time, sys 
 37  from Peach.publisher import Publisher 
 38   
 39  #__all__ = ['Raw'] 
 40   
41 -class Raw(Publisher):
42 ''' 43 A simple Raw publisher. 44 ''' 45 46 _host = None 47 _socket = None 48
49 - def __init__(self, interface, timeout = 0.1):
50 ''' 51 @type host: string 52 @param host: Remote host 53 @type timeout: number 54 @param timeout: How long to wait for reponse 55 ''' 56 self._interface = interface 57 self._timeout = float(timeout)
58
59 - def start(self):
60 ''' 61 Create connection. 62 ''' 63 pass
64
65 - def stop(self):
66 ''' 67 Close connection if open. 68 ''' 69 self.close()
70
71 - def connect(self):
72 if self._socket != None: 73 # Close out old socket first 74 self._socket.close() 75 self._socket = socket.socket(socket.AF_INET, socket.SOCK_RAW) 76 self._socket.bind((self._interface,0))
77
78 - def close(self):
79 if self._socket != None: 80 self._socket.close() 81 self._socket = None
82
83 - def send(self, data):
84 ''' 85 Send data via sendall. 86 87 @type data: string 88 @param data: Data to send 89 ''' 90 self._socket.sendall(data)
91
92 - def receive(self, size = None):
93 ''' 94 Receive upto 10000 bytes of data. 95 96 @rtype: string 97 @return: received data. 98 ''' 99 100 if size != None: 101 return self._socket.recv(size) 102 103 else: 104 self._socket.setblocking(0) 105 106 timeout = self._timeout 107 beginTime = time.time() 108 ret = '' 109 110 try: 111 while True: 112 if len(ret) > 0 or time.time() - beginTime > timeout: 113 break 114 115 try: 116 ret += self._socket.recv(10000) 117 except socket.error, e: 118 if str(e).find('The socket operation could not complete without blocking') == -1: 119 raise 120 121 except socket.error, e: 122 print "Socket:Receive(): Caught socket.error [%s]" % e 123 124 self._socket.setblocking(1) 125 return ret
126 127 # end 128