def setUp(self): self._server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self._server_connection = None self._bind() self._parent = CompositeNode() self._parent.configure({'name': 'parent', 'parent': None}) self._transporter = smtp_transporter.SMTPTransporter()
def connect(self, host='localhost', port=0, timeout=None): if timeout is None: timeout = self._timeout self._timeout = timeout if not port and (host.find(':') == host.rfind(':')): i = host.rfind(':') if i >= 0: host, port = host[:i], host[i + 1:] try: port = int(port) except ValueError: raise socket.error, "nonnumeric port" if not port: port = SMTP_PORT if self.debuglevel > 0: print 'connect:', (host, port) msg = "getaddrinfo returns an empty list" self.sock = None for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM): af, socktype, proto, canonname, sa = res try: if timeout is None: self.sock = socket.socket(af, socktype, proto) else: self.sock = socket.safety_socket(timeout, af, socktype, proto) if self.debuglevel > 0: print 'connect:', (host, port) self.sock.connect(sa) except socket.error, msg: if self.debuglevel > 0: print 'connect fail:', (host, port) if self.sock: self.sock.close() self.sock = None continue break
def create_listen_skt(port_num): """create_listen_skt(): Create/rtn TCP (stream) socket with given port num, to listen for remote connections.""" # AF_INET attr allows skt access via an Ethernet adapter, and SOCK_STREAM # specifies a reliable, connection-based (TCP) protocol: debug_print(0, "%s%s", g_str_create_listen_skt, 'Initializing listen_skt...') listen_skt = socket.socket(socket.AF_INET, socket.SOCK_STREAM) listen_skt.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) linger = struct.pack("ii", 1, 0) # - prevent skt from jabbering with empty # pkts after closure listen_skt.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, linger) debug_print(0, "%s listen_skt = %s", g_str_create_listen_skt, str(listen_skt)) # Bind the listen_skt object to a port (by specifying the port # number found in this node's config). The empty string used to specify # the host address causes the Python wrapper to send INADDR_ANY to the # underlying bind() system call, thereby enabling listening on the given # port on ALL adapters (eg ethX and serial/PPP): skt_address = ('', int(port_num)) listen_skt.bind(skt_address) debug_print(0, '%s listen_skt is bound to address %s.', g_str_create_listen_skt, str(skt_address)) # Set listen_skt to listen for connections from remote client skts. Allow # max of 3 queued connection requests (may change number for future uses): try: listen_skt.listen(3) except socket.error, details: debug_print(0, 'Call to socket.listen() failed: %s', details) return None
def connect(self, timeout=None): if timeout is None: timeout = self._timeout self._timeout = timeout msg = "getaddrinfo returns an empty list" for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM): af, socktype, proto, canonname, sa = res try: if self._timeout is None: self.sock = socket.socket(af, socktype, proto) else: self.sock = socket.safety_socket(self._timeout, af, socktype, proto) if self.debuglevel > 0: print "connect: (%s, %s)" % (self.host, self.port) self.sock.connect(sa) except socket.error, msg: if self.debuglevel > 0: print 'connect fail:', (self.host, self.port) if self.sock: self.sock.close() self.sock = None continue break
def setUp(self): self._server = socket.socket(socket.AF_INET,socket.SOCK_STREAM) self._server_connection = None self._bind() self._parent = CompositeNode() self._parent.configure({'name':'parent','parent':None}) self._transporter = smtp_transporter.SMTPTransporter()
def ntransfercmd(self, cmd, rest=None): timeout = self._timeout size = None if self.passiveserver: host, port = self.makepasv() tp = socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM)[0] af, socktype, proto, canon, sa = tp if timeout is None: conn = socket.socket(af, socktype, proto) else: conn = socket.safety_socket(timeout,af,socktype,proto) conn.connect(sa) if rest is not None: self.sendcmd("REST %s" % rest) resp = self.sendcmd(cmd) if resp[0] != '1': raise error_reply, resp else: sock = self.makeport() if rest is not None: self.sendcmd("REST %s" % rest) resp = self.sendcmd(cmd) if resp[0] != '1': raise error_reply, resp conn, sockaddr = sock.accept() if resp[:3] == '150': # this is conditional in case we received a 125 size = parse150(resp) return conn, size
def _clear_accept(self): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s.connect(('localhost', self.tcp_port)) s.close() except: pass
def open_connection(self): try: self._s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) except socket.error, e: msglog.log('Adura', ERR, 'Error creating socket - stopping protocol.') raise EResourceError
def connect(self,host='localhost',port=0,timeout=None): if timeout is None: timeout = self._timeout self._timeout = timeout if not port and (host.find(':') == host.rfind(':')): i = host.rfind(':') if i >= 0: host, port = host[:i], host[i+1:] try: port = int(port) except ValueError: raise socket.error, "nonnumeric port" if not port: port = SMTP_PORT if self.debuglevel > 0: print 'connect:', (host, port) msg = "getaddrinfo returns an empty list" self.sock = None for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM): af, socktype, proto, canonname, sa = res try: if timeout is None: self.sock = socket.socket(af, socktype, proto) else: self.sock = socket.safety_socket(timeout,af, socktype,proto) if self.debuglevel > 0: print 'connect:', (host, port) self.sock.connect(sa) except socket.error, msg: if self.debuglevel > 0: print 'connect fail:', (host, port) if self.sock: self.sock.close() self.sock = None continue break
def setUp(self): DefaultTestFixture.setUp(self) server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server_socket.bind(('localhost', 0)) # Bind to any available port on # localhost. server_socket.listen(5) sockname = server_socket.getsockname() ## # Socket ready to accept "eWebConnect" Alarm messages. self.server_socket = server_socket ## # The "eWebConnect" host. self.sever_address = sockname[0] ## # The "eWebConnect" post. self.server_port = sockname[1] self.new_node_tree() root = as_internal_node('/') self.configure({'name': 'Test Case', 'parent': '/'}) client = EWebConnectAlarmClient() client.configure({ 'name': 'eWebConnect', 'parent': self, 'host': 'localhost', 'port': self.server_port }) as_internal_node('/').start() return
def _send_alarm_dict(self, alarm_dict): if self.host is None: self.message_log('Failed to send alarm; host address is None:\n %r' % alarm_dict, msglog.types.INFO) return # nowhere to send it! ewebconnect_text = ( "%(timestamp)s, %(TZ)s, %(host)s, %(what)s, %(code)s:" " %(type)s %(text)s" ) % { "timestamp":self._alarm_timestamp(alarm_dict), "TZ":self._alarm_tz(alarm_dict), "host":self._alarm_host(alarm_dict), "what":self._alarm_what(alarm_dict), "code":self._alarm_code(alarm_dict), "type":self._alarm_type(alarm_dict), "text":self._alarm_text(alarm_dict), } self.message_log('Sending Alarm: %s' % ewebconnect_text) server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) try: # @fixme Try block only exists because the normal desctructor # invokation does not appear to work on mpx.lib.socket # sockets which is a big deal if there is an exception. # - mevans server_socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) server_socket.connect((self.host, self.port), self.timeout) server_socket.sendall(ewebconnect_text, self.timeout) finally: # A finally block is used because the normal desctructor invokation # does not appear to work on mpx.lib.socket socket's. - mevans # @fixme Figure out why! server_socket.close() # Could this hang? Should I use shutdown? self.message_log('Alarm Sent') return
def setUp(self): DefaultTestFixture.setUp(self) server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) server_socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) server_socket.bind(('localhost',0)) # Bind to any available port on # localhost. server_socket.listen(5) sockname = server_socket.getsockname() ## # Socket ready to accept "eWebConnect" Alarm messages. self.server_socket = server_socket ## # The "eWebConnect" host. self.sever_address = sockname[0] ## # The "eWebConnect" post. self.server_port = sockname[1] self.new_node_tree() root = as_internal_node('/') self.configure({'name':'Test Case', 'parent':'/'}) client = EWebConnectAlarmClient() client.configure({'name':'eWebConnect', 'parent':self, 'host':'localhost', 'port':self.server_port}) as_internal_node('/').start() return
def create_cmd_skts(class_name, _tmp_dir): # Establish an internal connection anchored by a pair of stream sockets by # which other threads may deliver cmds to this thread. (The actual cmd # calls wrap all access to the sockets and connection.): socket_name = os.path.join(_tmp_dir, (class_name + '.%d') % threading.gettid()) # Delete any existing file with the path & name of the socket to be # created: while os.path.exists(socket_name): try: os.remove(socket_name) except: socket_name += 'x' # Create UNIX listen_skt object: listen_skt = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) # Set this_socket to allow rebinding to different local connections while # a previous binding is "in the process" of disconnecting (can take up to # several minutes after binding is already disconnected...): try: listen_skt.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) except Exception, e: debug_print(0, '%s Set reuse failed, %s', g_str_create_cmd_skts, e)
def _send_alarm_dict(self, alarm_dict): if self.host is None: self.message_log( 'Failed to send alarm; host address is None:\n %r' % alarm_dict, msglog.types.INFO) return # nowhere to send it! ewebconnect_text = ( "%(timestamp)s, %(TZ)s, %(host)s, %(what)s, %(code)s:" " %(type)s %(text)s") % { "timestamp": self._alarm_timestamp(alarm_dict), "TZ": self._alarm_tz(alarm_dict), "host": self._alarm_host(alarm_dict), "what": self._alarm_what(alarm_dict), "code": self._alarm_code(alarm_dict), "type": self._alarm_type(alarm_dict), "text": self._alarm_text(alarm_dict), } self.message_log('Sending Alarm: %s' % ewebconnect_text) server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: # @fixme Try block only exists because the normal desctructor # invokation does not appear to work on mpx.lib.socket # sockets which is a big deal if there is an exception. # - mevans server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server_socket.connect((self.host, self.port), self.timeout) server_socket.sendall(ewebconnect_text, self.timeout) finally: # A finally block is used because the normal desctructor invokation # does not appear to work on mpx.lib.socket socket's. - mevans # @fixme Figure out why! server_socket.close() # Could this hang? Should I use shutdown? self.message_log('Alarm Sent') return
def setUp(self): self._server = socket.socket(socket.AF_INET,socket.SOCK_STREAM) self._server_connection = None self._bind() parent = CompositeNode() parent.configure({'name':'parent','parent':None}) self._transporter = http_post_transporter.HTTPPostTransporter() self._transporter.configure({'name':'transporter','parent':parent, 'post_url':'http://localhost:%s/test' % self._port,'timeout':'1'})
def open_socket(self): #call to start or restart the connection to the CPC device if self.debug: print 'enter open_socket' try: self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #, self.timeout) result = self.socket.connect((self.parent.ip, self.parent.port), self.timeout) time.sleep(2) if self.debug: 'socket opened' return 1 except: msglog.exception() return 0
def open_socket( self): #call to start or restart the connection to the CPC device if self.debug: print 'enter open_socket' try: self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #, self.timeout) result = self.socket.connect((self.parent.ip, self.parent.port), self.timeout) time.sleep(2) if self.debug: 'socket opened' return 1 except: msglog.exception() return 0
def connect(self, timeout=None): if timeout is None: timeout = self._timeout self._timeout = timeout if self._timeout is None: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) else: sock = socket.safety_socket(self._timeout, socket.AF_INET, socket.SOCK_STREAM) sock.connect((self.host, self.port)) realsock = sock if hasattr(sock, '_sock'): realsock = sock._sock ssl = socket.ssl(realsock, self.key_file, self.cert_file) self.sock = FakeSocket(sock, ssl)
def connect(self,timeout=None): if timeout is None: timeout = self._timeout self._timeout = timeout if self._timeout is None: sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM) else: sock = socket.safety_socket(self._timeout, socket.AF_INET, socket.SOCK_STREAM) sock.connect((self.host,self.port)) realsock = sock if hasattr(sock,'_sock'): realsock = sock._sock ssl = socket.ssl(realsock,self.key_file,self.cert_file) self.sock = FakeSocket(sock,ssl)
def setUp(self): self._server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self._server_connection = None self._bind() parent = CompositeNode() parent.configure({'name': 'parent', 'parent': None}) self._transporter = http_post_transporter.HTTPPostTransporter() self._transporter.configure({ 'name': 'transporter', 'parent': parent, 'post_url': 'http://localhost:%s/test' % self._port, 'timeout': '1' })
def makeport(self): timeout = self._timeout msg = "getaddrinfo returns an empty list" sock = None for res in socket.getaddrinfo(None, 0, self.af, socket.SOCK_STREAM, 0, socket.AI_PASSIVE): af, socktype, proto, canonname, sa = res try: if timeout is None: sock = socket.socket(af, socktype, proto) else: sock = socket.safety_socket(timeout,af, socktype,proto) sock.bind(sa) except socket.error, msg: if sock: sock.close() sock = None continue break
def connect(self,host='',port=0,timeout=None): if timeout is None: timeout = self._timeout self._timeout = timeout if host: self.host = host if port: self.port = port self.passiveserver = 0 msg = "getaddrinfo returns an empty list" for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM): af, socktype, proto, canonname, sa = res try: if timeout is None: self.sock = socket.socket(af, socktype, proto) else: self.sock = socket.safety_socket(timeout,af, socktype,proto) self.sock.connect(sa) except socket.error, msg: if self.sock: self.sock.close() self.sock = None continue break
def connect(self,timeout=None): if timeout is None: timeout = self._timeout self._timeout = timeout msg = "getaddrinfo returns an empty list" for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM): af, socktype, proto, canonname, sa = res try: if self._timeout is None: self.sock = socket.socket(af, socktype, proto) else: self.sock = socket.safety_socket(self._timeout, af, socktype, proto) if self.debuglevel > 0: print "connect: (%s, %s)" % (self.host, self.port) self.sock.connect(sa) except socket.error, msg: if self.debuglevel > 0: print 'connect fail:', (self.host, self.port) if self.sock: self.sock.close() self.sock = None continue break
def open_connection(self): self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def setUp(self): self._server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self._client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self._server_connection = None self._bind()
As a special exception, if other files instantiate classes, templates or use macros or inline functions from this project, or you compile this file and link it with other works to produce a work based on this file, this file does not by itself cause the resulting work to be covered by the GNU General Public License. However the source code for this file must still be made available in accordance with section (3) of the GNU General Public License. This exception does not invalidate any other reasons why a work based on this file might be covered by the GNU General Public License. """ import struct from mpx.lib import socket listen_skt = socket.socket(socket.AF_INET, socket.SOCK_STREAM) listen_skt.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) linger = struct.pack( "ii", 1, 0) # prevent skt from jabbering with empty pkts after closure listen_skt.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, linger) skt_address = ('', 8080) print 'A' listen_skt.bind(skt_address) try: listen_skt.listen(1) except Exception, e: print 'Call to socket.listen() failed: %s' % str(e) while 1: print 'B' conn, addr = listen_skt.accept() print 'C'
def _create_socket(self): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) return s
As a special exception, if other files instantiate classes, templates or use macros or inline functions from this project, or you compile this file and link it with other works to produce a work based on this file, this file does not by itself cause the resulting work to be covered by the GNU General Public License. However the source code for this file must still be made available in accordance with section (3) of the GNU General Public License. This exception does not invalidate any other reasons why a work based on this file might be covered by the GNU General Public License. """ import struct from mpx.lib import socket listen_skt = socket.socket(socket.AF_INET, socket.SOCK_STREAM) listen_skt.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) linger = struct.pack("ii", 1, 0) # prevent skt from jabbering with empty pkts after closure listen_skt.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, linger) skt_address = ('', 8080) print 'A' listen_skt.bind(skt_address) try: listen_skt.listen(1) except Exception, e: print 'Call to socket.listen() failed: %s' % str(e) while 1: print 'B' conn, addr = listen_skt.accept() print 'C' conn.setblocking(1)
def open_connection(self): self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #, self.timeout) answer = self.socket.connect((self.ip, self.port), self.timeout) return answer
def setUp(self): self._server = socket.socket(socket.AF_INET,socket.SOCK_STREAM) self._server_connection = None self._bind()
def open_connection(self): try: self._s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) except socket.error, e: log('Error creating socket - stopping protocol', ERR, self.debug) raise EResourceError
# several minutes after binding is already disconnected...): try: listen_skt.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) except Exception, e: debug_print(0, '%s Set reuse failed, %s', g_str_create_cmd_skts, e) # Assign the created socket name/address to the listen socket object: listen_skt.bind(socket_name) # Create the actual sockets for each end of the command connection, # and establish a connection between the 2 sockets: _far_cmd_skt = None _near_cmd_skt = None try: listen_skt.listen(1) # only want one connection (ie to far_skt) _far_cmd_skt = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) _far_cmd_skt.connect(socket_name) _near_cmd_skt, addr = listen_skt.accept() _near_cmd_skt.setblocking(0) # no blocking on cmd skt connection finally: # Once the connection is established, we can delete the file. # It will be removed from its directory, but continue to exist # until this process is no longer connected to it. os.remove(socket_name) debug_print(1, '%s Cmd skt created', g_str_create_cmd_skts) return (_far_cmd_skt, _near_cmd_skt) def form_net_pkt(dst_addr, type, data, ext = None, flags = 0): debug_print(1, 'Forming net pkt with dst_addr = %d, type = %d, data = %d', dst_addr, type, data)
# several minutes after binding is already disconnected...): try: listen_skt.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) except Exception, e: debug_print(0, '%s Set reuse failed, %s', g_str_create_cmd_skts, e) # Assign the created socket name/address to the listen socket object: listen_skt.bind(socket_name) # Create the actual sockets for each end of the command connection, # and establish a connection between the 2 sockets: _far_cmd_skt = None _near_cmd_skt = None try: listen_skt.listen(1) # only want one connection (ie to far_skt) _far_cmd_skt = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) _far_cmd_skt.connect(socket_name) _near_cmd_skt, addr = listen_skt.accept() _near_cmd_skt.setblocking(0) # no blocking on cmd skt connection finally: # Once the connection is established, we can delete the file. # It will be removed from its directory, but continue to exist # until this process is no longer connected to it. os.remove(socket_name) debug_print(1, '%s Cmd skt created', g_str_create_cmd_skts) return (_far_cmd_skt, _near_cmd_skt) def form_net_pkt(dst_addr, type, data, ext=None, flags=0): debug_print(1, 'Forming net pkt with dst_addr = %d, type = %d, data = %d', dst_addr, type, data)