示例#1
0
	def __init__(self, s, address, debug=False):
		Connection.__init__(self)

		self.address = address
		self.setup(s, debug=debug, nb=True)

		self.poll = self.initalpoll
示例#2
0
	def __init__(self, host=None, port=None, nb=0, debug=False):
		Connection.__init__(self)

		self.buffered['undescribed'] = {}
		self.buffered['store'] = {}

		if host != None:
			self.setup(host, port, nb, debug)

		self.__desc = False
class ClientConnection(Connection):
    """
	Now with thread safe goodness! *Yay*!
	Now with readable and understandable code! *Yay*
	"""
    def setup(self, host, port=None, debug=False, proxy=None):
        """\
		*Internal*

		Sets up the socket for a connection.
		"""
        hoststring = host
        self.proxy = None

        if hoststring.startswith("tphttp://") or hoststring.startswith(
                "tphttps://"):
            hoststring = hoststring[2:]

        if hoststring.startswith("http://") or hoststring.startswith(
                "https://"):
            import urllib
            opener = None

            # use enviroment varibles
            if proxy == None:
                opener = urllib.FancyURLopener()
            elif proxy == "":
                # Don't use any proxies
                opener = urllib.FancyURLopener({})
            else:
                if hoststring.startswith("http://"):
                    opener = urlib.FancyURLopener({'http': proxy})
                elif hoststring.startswith("https://"):
                    opener = urlib.FancyURLopener({'https': proxy})
                else:
                    raise "URL Error..."

            import random, string
            url = "/"
            for i in range(0, 12):
                url += random.choice(string.letters + string.digits)

            o = opener.open(hoststring + url, "")
            s = socket.fromfd(o.fileno(), socket.AF_INET, socket.SOCK_STREAM)

##			# Read in the headers
##			buffer = ""
##			while not buffer.endswith("\r\n\r\n"):
##				print "buffer:", repr(buffer)
##				try:
##					buffer += s.recv(1)
##				except socket.error, e:
##					pass
##			print "Finished the http headers..."

        else:
            if hoststring.startswith("tp://") or hoststring.startswith(
                    "tps://"):
                if hoststring.startswith("tp://"):
                    host = hoststring[5:]
                    if not port:
                        port = 6923
                elif hoststring.startswith("tps://"):
                    host = hoststring[6:]
                    if not port:
                        port = 6924

                if host.count(":") > 0:
                    host, port = host.split(':', 1)
                    port = int(port)
            else:
                if hoststring.count(":") > 0:
                    host, port = hoststring.split(':', 1)
                    port = int(port)
                else:
                    host = hoststring

                    if not port:
                        port = 6923

            print "Connecting to", host, type(host), port, type(port)

            s = None
            for af, socktype, proto, cannoname, sa in \
              socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):

                try:
                    s = socket.socket(af, socktype, proto)
                    if debug:
                        print "Trying to connect to connect: (%s, %s)" % (host,
                                                                          port)

                    s.connect(sa)
                    break
                except socket.error, msg:
                    if debug:
                        print "Connect fail: (%s, %s)" % (host, port)
                    if s:
                        s.close()

                    s = None
                    continue

            if not s:
                raise socket.error, msg

            if hoststring.startswith("tps://"):
                print "Creating SSL wrapper...."
                s = SSLWrapper(s)

        self.hoststring = hoststring
        self.host = host
        self.port = port

        Connection.setup(self, s, debug=debug)