Example #1
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)
Example #2
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)
Example #3
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
Example #4
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