Esempio n. 1
0
 def open(self, sessiondata=None):
     """create a UDP socket to SNMP agent"""
     if isinstance(sessiondata, sessionData):
         self.sessiondata = sessiondata
     if self.sessiondata:
         self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
         self.socket.connect((self.sessiondata.agent, self.sessiondata.port))
Esempio n. 2
0
 def open(self, sessiondata=None):
     """create a UDP socket to SNMP agent"""
     if isinstance(sessiondata, sessionData):
         self.sessiondata = sessiondata
     if self.sessiondata:
         self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
         self.socket.connect((self.sessiondata.agent, self.sessiondata.port))
Esempio n. 3
0
 def __init__(self, port=9025, logfile=None, parser=None):
     self.port = port
     self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
     self.file = None
     self.conn = None
     self.envelopes = []
     self.hostname = socket.gethostname()
     self._client = None
     self._callback = None
     self._reset()
     while 1:
         try:
             self.socket.bind(("", self.port))
             break
         except socket.error as msg:
             print("couldn't bind port",
                   self.port,
                   " - ",
                   msg,
                   "retrying...",
                   file=sys.stderr)
         scheduler.sleep(5)
     self.socket.listen(50)
     self.conversation = None
     self.logfile = logfile
     self.parser = parser
Esempio n. 4
0
    def connect(self):
        "Connect to a host on a given (SSL) port."

        sock = socket.socket(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)
Esempio n. 5
0
    def connect(self):
        "Connect to a host on a given (SSL) port."

        sock = socket.socket(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)
Esempio n. 6
0
    def connect(self, host='localhost', port=0, bindto=None, retries=3):
        """Connect to a host on a given port.

        If the hostname ends with a colon (`:`) followed by a number, and
        there is no port specified, that suffix will be stripped off and the
        number interpreted as the port number to use.

        Note: This method is automatically invoked by __init__, if a host is
        specified during instantiation.

        """
        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.logfile:
            self.logfile.write('attempting SMTP.connect: %s %d\n' %
                               (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:
                self.sock = socket.socket(af, socktype, proto)
                self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
                if self.logfile:
                    self.logfile.write('SMTP.connect: %s %d\n' % (host, port))
                if bindto:
                    self.sock.bind((bindto, socket.IPPORT_USERRESERVED))
                    self._bindto = bindto
                self._connect(sa, retries)
            except socket.error as msg:
                if self.logfile:
                    self.logfile.write('SMTP.connect fail: %s %d\n' %
                                       (host, port))
                if self.sock:
                    self.sock.close()
                self.sock = None
                continue
            break
        if not self.sock:
            raise socket.error(msg)
        (code, msg) = self.getreply()
        if self.logfile:
            self.logfile.write('SMTP.connect: %s %d\n' % (host, port))
        return (code, msg)
Esempio n. 7
0
    def connect(self, host='localhost', port=0, bindto=None, retries=3):
        """Connect to a host on a given port.

        If the hostname ends with a colon (`:`) followed by a number, and
        there is no port specified, that suffix will be stripped off and the
        number interpreted as the port number to use.

        Note: This method is automatically invoked by __init__, if a host is
        specified during instantiation.

        """
        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.logfile:
            self.logfile.write('attempting SMTP.connect: %s %d\n' % (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:
                self.sock = socket.socket(af, socktype, proto)
                self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
                if self.logfile:
                    self.logfile.write('SMTP.connect: %s %d\n' % (host, port))
                if bindto:
                    self.sock.bind((bindto, socket.IPPORT_USERRESERVED))
                    self._bindto = bindto
                self._connect(sa, retries)
            except socket.error as msg:
                if self.logfile:
                    self.logfile.write('SMTP.connect fail: %s %d\n' % (host, port))
                if self.sock:
                    self.sock.close()
                self.sock = None
                continue
            break
        if not self.sock:
            raise socket.error(msg)
        (code, msg) = self.getreply()
        if self.logfile:
            self.logfile.write('SMTP.connect: %s %d\n' % (host, port))
        return (code, msg)
Esempio n. 8
0
    def connect(self):
        """Connect to the host and port specified in __init__."""
        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:
                self.sock = socket.socket(af, socktype, proto)
#               self.sock = asyncio.SocketDispatcher()
#               self.sock.create_socket(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
Esempio n. 9
0
 def connect(self):
     """Connect to the host and port specified in __init__."""
     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:
             self.sock = socket.socket(af, socktype, proto)
             #               self.sock = asyncio.SocketDispatcher()
             #               self.sock.create_socket(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
Esempio n. 10
0
 def __init__ (self, port=9025, logfile=None, parser=None):
     self.port = port
     self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
     self.file = None
     self.conn = None
     self.envelopes = []
     self.hostname = socket.gethostname()
     self._client = None
     self._callback = None
     self._reset()
     while 1:
         try:
             self.socket.bind(("", self.port))
             break
         except socket.error as msg:
             print ("couldn't bind port",self.port," - ",msg,"retrying...", file=sys.stderr)
         scheduler.sleep(5)
     self.socket.listen(50)
     self.conversation = None
     self.logfile = logfile
     self.parser = parser
Esempio n. 11
0
    def _setupSocket(self):
        if self._bindAddress is None: # Run as a normal FastCGI?

            sock = socket.fromfd(FCGI_LISTENSOCK_FILENO, socket.AF_INET,
                                 socket.SOCK_STREAM)
            try:
                sock.getpeername()
            except socket.error, e:
                if e[0] != errno.ENOTCONN:
                    raise
        else:
            # Run as a server
            oldUmask = None
            if isinstance(self._bindAddress, str):
                # Unix socket
                sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
                try:
                    os.unlink(self._bindAddress)
                except OSError:
                    pass
                if self._umask is not None:
                    oldUmask = os.umask(self._umask)
            else:
                # INET socket
                assert type(self._bindAddress) is tuple
                assert len(self._bindAddress) == 2
                sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            close_on_exec(sock.fileno())
            sock.bind(self._bindAddress)
            sock.listen(socket.SOMAXCONN)
Esempio n. 12
0
 def __init__(self):
     # create a socket so we have a handle to query
     self.sockfd = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
Esempio n. 13
0
    def _setupSocket(self):
        if self._bindAddress is None:  # Run as a normal FastCGI?

            sock = socket.fromfd(FCGI_LISTENSOCK_FILENO, socket.AF_INET,
                                 socket.SOCK_STREAM)
            try:
                sock.getpeername()
            except socket.error, e:
                if e[0] != errno.ENOTCONN:
                    raise
        else:
            # Run as a server
            oldUmask = None
            if type(self._bindAddress) is str:
                # Unix socket
                sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
                try:
                    os.unlink(self._bindAddress)
                except OSError:
                    pass
                if self._umask is not None:
                    oldUmask = os.umask(self._umask)
            else:
                # INET socket
                assert type(self._bindAddress) is tuple
                assert len(self._bindAddress) == 2
                sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

            sock.bind(self._bindAddress)
            sock.listen(socket.SOMAXCONN)
Esempio n. 14
0
 def __init__(self):
     # create a socket so we have a handle to query
     self.sockfd = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)