Exemplo n.º 1
0
 def get_names(self):
     if FileHandler.names is None:
         try:
             FileHandler.names = (socket.gethostbyname('localhost'),
                                 socket.gethostbyname(socket.gethostname()))
         except socket.gaierror:
             FileHandler.names = (socket.gethostbyname('localhost'),)
     return FileHandler.names
Exemplo n.º 2
0
 def open_local_file(self, url):
     """Use local file."""
     import mimetypes, mimetools, email.Utils
     try:
         from io import StringIO
     except ImportError:
         from io import StringIO
     host, file = splithost(url)
     localname = url2pathname(file)
     try:
         stats = os.stat(localname)
     except OSError as e:
         raise IOError(e.errno, e.strerror, e.filename)
     size = stats.st_size
     modified = email.Utils.formatdate(stats.st_mtime, usegmt=True)
     mtype = mimetypes.guess_type(url)[0]
     headers = mimetools.Message(
         StringIO(
             'Content-Type: %s\nContent-Length: %d\nLast-modified: %s\n' %
             (mtype or 'text/plain', size, modified)))
     if not host:
         urlfile = file
         if file[:1] == '/':
             urlfile = 'file://' + file
         return addinfourl(open(localname, 'rb'), headers, urlfile)
     host, port = splitport(host)
     if not port \
        and socket.gethostbyname(host) in (localhost(), thishost()):
         urlfile = file
         if file[:1] == '/':
             urlfile = 'file://' + file
         return addinfourl(open(localname, 'rb'), headers, urlfile)
     raise IOError('local file error', 'not on local host')
Exemplo n.º 3
0
    def ftp_open(self, req):
        from eventlib.green import ftplib
        import mimetypes
        host = req.get_host()
        if not host:
            raise IOError, ('ftp error', 'no host given')
        host, port = splitport(host)
        if port is None:
            port = ftplib.FTP_PORT
        else:
            port = int(port)

        # username/password handling
        user, host = splituser(host)
        if user:
            user, passwd = splitpasswd(user)
        else:
            passwd = None
        host = unquote(host)
        user = unquote(user or '')
        passwd = unquote(passwd or '')

        try:
            host = socket.gethostbyname(host)
        except socket.error, msg:
            raise URLError(msg)
Exemplo n.º 4
0
def geturl(url):
    c = socket.socket()
    ip = socket.gethostbyname(url)
    c.connect((ip, 80))
    print '%s connected' % url
    c.send('GET /\r\n\r\n')
    return c.recv(1024)
Exemplo n.º 5
0
def geturl(url):
    sock = socket
    c = sock.socket()
    ip = socket.gethostbyname(url)
    #conn = c.connect((ip, 80))
    #print('%s connected %s' % (url, ip))
    #sent = c.send('GET /\r\n\r\n')
    return "#"  #c.recv(1024)
Exemplo n.º 6
0
 def testSockName(self):
     # Testing getsockname()
     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     sock.bind(("0.0.0.0", PORT + 1))
     name = sock.getsockname()
     # XXX(nnorwitz): http://tinyurl.com/os5jz seems to indicate
     # it reasonable to get the host's addr in addition to 0.0.0.0.
     # At least for eCos.  This is required for the S/390 to pass.
     my_ip_addr = socket.gethostbyname(socket.gethostname())
     self.assert_(name[0] in ("0.0.0.0", my_ip_addr),
                  '%s invalid' % name[0])
     self.assertEqual(name[1], PORT + 1)
Exemplo n.º 7
0
    def ftp_open(self, req):
        from eventlib.green import ftplib
        import mimetypes
        host = req.get_host()
        if not host:
            raise IOError('ftp error', 'no host given')
        host, port = splitport(host)
        if port is None:
            port = ftplib.FTP_PORT
        else:
            port = int(port)

        # username/password handling
        user, host = splituser(host)
        if user:
            user, passwd = splitpasswd(user)
        else:
            passwd = None
        host = unquote(host)
        user = unquote(user or '')
        passwd = unquote(passwd or '')

        try:
            host = socket.gethostbyname(host)
        except socket.error as msg:
            raise URLError(msg)
        path, attrs = splitattr(req.get_selector())
        dirs = path.split('/')
        dirs = list(map(unquote, dirs))
        dirs, file = dirs[:-1], dirs[-1]
        if dirs and not dirs[0]:
            dirs = dirs[1:]
        try:
            fw = self.connect_ftp(user, passwd, host, port, dirs)
            type = file and 'I' or 'D'
            for attr in attrs:
                attr, value = splitvalue(attr)
                if attr.lower() == 'type' and \
                   value in ('a', 'A', 'i', 'I', 'd', 'D'):
                    type = value.upper()
            fp, retrlen = fw.retrfile(file, type)
            headers = ""
            mtype = mimetypes.guess_type(req.get_full_url())[0]
            if mtype:
                headers += "Content-type: %s\n" % mtype
            if retrlen is not None and retrlen >= 0:
                headers += "Content-length: %d\n" % retrlen
            sf = StringIO(headers)
            headers = mimetools.Message(sf)
            return addinfourl(fp, headers, req.get_full_url())
        except ftplib.all_errors as msg:
            raise IOError('ftp error', msg).with_traceback(sys.exc_info()[2])
Exemplo n.º 8
0
    def test_ftp(self):
        class MockFTPWrapper:
            def __init__(self, data):
                self.data = data

            def retrfile(self, filename, filetype):
                self.filename, self.filetype = filename, filetype
                return io.StringIO(self.data), len(self.data)

        class NullFTPHandler(urllib.request.FTPHandler):
            def __init__(self, data):
                self.data = data

            def connect_ftp(self, user, passwd, host, port, dirs):
                self.user, self.passwd = user, passwd
                self.host, self.port = host, port
                self.dirs = dirs
                self.ftpwrapper = MockFTPWrapper(self.data)
                return self.ftpwrapper

        import ftplib, socket
        data = "rheum rhaponicum"
        h = NullFTPHandler(data)
        o = h.parent = MockOpener()

        for url, host, port, type_, dirs, filename, mimetype in [
            ("ftp://localhost/foo/bar/baz.html", "localhost", ftplib.FTP_PORT,
             "I", ["foo", "bar"], "baz.html", "text/html"),
            ("ftp://localhost:80/foo/bar/", "localhost", 80, "D",
             ["foo", "bar"], "", None),
            ("ftp://localhost/baz.gif;type=a", "localhost", ftplib.FTP_PORT,
             "A", [], "baz.gif",
             None),  # XXX really this should guess image/gif
        ]:
            r = h.ftp_open(Request(url))
            # ftp authentication not yet implemented by FTPHandler
            self.assertTrue(h.user == h.passwd == "")
            self.assertEqual(h.host, socket.gethostbyname(host))
            self.assertEqual(h.port, port)
            self.assertEqual(h.dirs, dirs)
            self.assertEqual(h.ftpwrapper.filename, filename)
            self.assertEqual(h.ftpwrapper.filetype, type_)
            headers = r.info()
            self.assertEqual(headers.get("Content-type"), mimetype)
            self.assertEqual(int(headers["Content-length"]), len(data))
Exemplo n.º 9
0
    def prepare(self, local_uri=None):
        """Start listening for an incoming MSRP connection using port and
        use_tls from local_uri if provided.

        Return full local path, suitable to put in SDP a:path attribute.
        Note, that `local_uri' may be updated in place.
        """
        local_uri = local_uri or protocol.URI(port=0)
        self.transport_event = coros.event()
        local_uri.host = gethostbyname(local_uri.host)
        factory = SpawnFactory(self.transport_event,
                               MSRPTransport,
                               local_uri,
                               logger=self.logger,
                               use_sessmatch=self.use_sessmatch)
        self.listening_port = self._listen(local_uri, factory)
        self.local_uri = local_uri
        return [local_uri]
Exemplo n.º 10
0
 def open_local_file(self, req):
     import email.Utils
     import mimetypes
     host = req.get_host()
     file = req.get_selector()
     localfile = url2pathname(file)
     stats = os.stat(localfile)
     size = stats.st_size
     modified = email.Utils.formatdate(stats.st_mtime, usegmt=True)
     mtype = mimetypes.guess_type(file)[0]
     headers = mimetools.Message(StringIO(
         'Content-type: %s\nContent-length: %d\nLast-modified: %s\n' %
         (mtype or 'text/plain', size, modified)))
     if host:
         host, port = splitport(host)
     if not host or \
        (not port and socket.gethostbyname(host) in self.get_names()):
         return addinfourl(open(localfile, 'rb'),
                           headers, 'file:'+file)
     raise URLError('file not on local host')
Exemplo n.º 11
0
 def testHostnameRes(self):
     # Testing hostname resolution mechanisms
     hostname = socket.gethostname()
     try:
         ip = socket.gethostbyname(hostname)
     except socket.error:
         # Probably name lookup wasn't set up right; skip this test
         return
     self.assert_(ip.find('.') >= 0, "Error resolving host to ip.")
     try:
         hname, aliases, ipaddrs = socket.gethostbyaddr(ip)
     except socket.error:
         # Probably a similar problem as above; skip this test
         return
     all_host_names = [hostname, hname] + aliases
     fqhn = socket.getfqdn(ip)
     if not fqhn in all_host_names:
         self.fail(
             "Error testing host resolution mechanisms. (fqdn: %s, all: %s)"
             % (fqhn, repr(all_host_names)))
Exemplo n.º 12
0
 def open_ftp(self, url):
     """Use FTP protocol."""
     if not isinstance(url, str):
         raise IOError, ('ftp error', 'proxy support for ftp protocol currently not implemented')
     import mimetypes, mimetools
     try:
         from cStringIO import StringIO
     except ImportError:
         from StringIO import StringIO
     host, path = splithost(url)
     if not host: raise IOError, ('ftp error', 'no host given')
     host, port = splitport(host)
     user, host = splituser(host)
     if user: user, passwd = splitpasswd(user)
     else: passwd = None
     host = unquote(host)
     user = unquote(user or '')
     passwd = unquote(passwd or '')
     host = socket.gethostbyname(host)
     if not port:
         from eventlib.green import ftplib
         port = ftplib.FTP_PORT
     else:
         port = int(port)
     path, attrs = splitattr(path)
     path = unquote(path)
     dirs = path.split('/')
     dirs, file = dirs[:-1], dirs[-1]
     if dirs and not dirs[0]: dirs = dirs[1:]
     if dirs and not dirs[0]: dirs[0] = '/'
     key = user, host, port, '/'.join(dirs)
     # XXX thread unsafe!
     if len(self.ftpcache) > MAXFTPCACHE:
         # Prune the cache, rather arbitrarily
         for k in self.ftpcache.keys():
             if k != key:
                 v = self.ftpcache[k]
                 del self.ftpcache[k]
                 v.close()
     try:
         if not key in self.ftpcache:
             self.ftpcache[key] = \
                 ftpwrapper(user, passwd, host, port, dirs)
         if not file: type = 'D'
         else: type = 'I'
         for attr in attrs:
             attr, value = splitvalue(attr)
             if attr.lower() == 'type' and \
                value in ('a', 'A', 'i', 'I', 'd', 'D'):
                 type = value.upper()
         (fp, retrlen) = self.ftpcache[key].retrfile(file, type)
         mtype = mimetypes.guess_type("ftp:" + url)[0]
         headers = ""
         if mtype:
             headers += "Content-Type: %s\n" % mtype
         if retrlen is not None and retrlen >= 0:
             headers += "Content-Length: %d\n" % retrlen
         headers = mimetools.Message(StringIO(headers))
         return addinfourl(fp, headers, "ftp:" + url)
     except ftperrors(), msg:
         raise IOError, ('ftp error', msg), sys.exc_info()[2]
Exemplo n.º 13
0
def thishost():
    """Return the IP address of the current host."""
    global _thishost
    if _thishost is None:
        _thishost = socket.gethostbyname(socket.gethostname())
    return _thishost
Exemplo n.º 14
0
def localhost():
    """Return the IP address of the magic hostname 'localhost'."""
    global _localhost
    if _localhost is None:
        _localhost = socket.gethostbyname('localhost')
    return _localhost
Exemplo n.º 15
0
 def open_ftp(self, url):
     """Use FTP protocol."""
     if not isinstance(url, str):
         raise IOError(
             'ftp error',
             'proxy support for ftp protocol currently not implemented')
     import mimetypes, mimetools
     try:
         from io import StringIO
     except ImportError:
         from io import StringIO
     host, path = splithost(url)
     if not host: raise IOError('ftp error', 'no host given')
     host, port = splitport(host)
     user, host = splituser(host)
     if user: user, passwd = splitpasswd(user)
     else: passwd = None
     host = unquote(host)
     user = unquote(user or '')
     passwd = unquote(passwd or '')
     host = socket.gethostbyname(host)
     if not port:
         from eventlib.green import ftplib
         port = ftplib.FTP_PORT
     else:
         port = int(port)
     path, attrs = splitattr(path)
     path = unquote(path)
     dirs = path.split('/')
     dirs, file = dirs[:-1], dirs[-1]
     if dirs and not dirs[0]: dirs = dirs[1:]
     if dirs and not dirs[0]: dirs[0] = '/'
     key = user, host, port, '/'.join(dirs)
     # XXX thread unsafe!
     if len(self.ftpcache) > MAXFTPCACHE:
         # Prune the cache, rather arbitrarily
         for k in list(self.ftpcache.keys()):
             if k != key:
                 v = self.ftpcache[k]
                 del self.ftpcache[k]
                 v.close()
     try:
         if not key in self.ftpcache:
             self.ftpcache[key] = \
                 ftpwrapper(user, passwd, host, port, dirs)
         if not file: type = 'D'
         else: type = 'I'
         for attr in attrs:
             attr, value = splitvalue(attr)
             if attr.lower() == 'type' and \
                value in ('a', 'A', 'i', 'I', 'd', 'D'):
                 type = value.upper()
         (fp, retrlen) = self.ftpcache[key].retrfile(file, type)
         mtype = mimetypes.guess_type("ftp:" + url)[0]
         headers = ""
         if mtype:
             headers += "Content-Type: %s\n" % mtype
         if retrlen is not None and retrlen >= 0:
             headers += "Content-Length: %d\n" % retrlen
         headers = mimetools.Message(StringIO(headers))
         return addinfourl(fp, headers, "ftp:" + url)
     except ftperrors() as msg:
         raise IOError('ftp error', msg).with_traceback(sys.exc_info()[2])
Exemplo n.º 16
0
def localhost():
    """Return the IP address of the magic hostname 'localhost'."""
    global _localhost
    if _localhost is None:
        _localhost = socket.gethostbyname('localhost')
    return _localhost
Exemplo n.º 17
0
# from eventlib.api import sleep
#
# i = 0
# while i < 100:
#     sleep(0.1)
#     print('%d. Sleep .1 second' % i)
#     i = i + 1

from time import perf_counter
from eventlib.green.socket import gethostbyname

st = perf_counter()
for i in range(100):
    print(gethostbyname('ag-projects.com'))
print('Time used: {}s'.format(perf_counter() - st))
Exemplo n.º 18
0
def thishost():
    """Return the IP address of the current host."""
    global _thishost
    if _thishost is None:
        _thishost = socket.gethostbyname(socket.gethostname())
    return _thishost
Exemplo n.º 19
0
    def test_file(self):
        import rfc822
        h = urllib.request.FileHandler()
        o = h.parent = MockOpener()

        TESTFN = test_support.TESTFN
        urlpath = sanepathname2url(os.path.abspath(TESTFN))
        towrite = "hello, world\n"
        urls = [
            "file://localhost%s" % urlpath,
            "file://%s" % urlpath,
            "file://%s%s" % (socket.gethostbyname('localhost'), urlpath),
        ]
        try:
            localaddr = socket.gethostbyname(socket.gethostname())
        except socket.gaierror:
            localaddr = ''
        if localaddr:
            urls.append("file://%s%s" % (localaddr, urlpath))

        for url in urls:
            f = open(TESTFN, "wb")
            try:
                try:
                    f.write(towrite)
                finally:
                    f.close()

                r = h.file_open(Request(url))
                try:
                    data = r.read()
                    headers = r.info()
                    newurl = r.geturl()
                finally:
                    r.close()
                stats = os.stat(TESTFN)
                modified = rfc822.formatdate(stats.st_mtime)
            finally:
                os.remove(TESTFN)
            self.assertEqual(data, towrite)
            self.assertEqual(headers["Content-type"], "text/plain")
            self.assertEqual(headers["Content-length"], "13")
            self.assertEqual(headers["Last-modified"], modified)

        for url in [
                "file://localhost:80%s" % urlpath,
                # XXXX bug: these fail with socket.gaierror, should be URLError
                ##             "file://%s:80%s/%s" % (socket.gethostbyname('localhost'),
                ##                                    os.getcwd(), TESTFN),
                ##             "file://somerandomhost.ontheinternet.com%s/%s" %
                ##             (os.getcwd(), TESTFN),
        ]:
            try:
                f = open(TESTFN, "wb")
                try:
                    f.write(towrite)
                finally:
                    f.close()

                self.assertRaises(urllib.error.URLError, h.file_open,
                                  Request(url))
            finally:
                os.remove(TESTFN)

        h = urllib.request.FileHandler()
        o = h.parent = MockOpener()
        # XXXX why does // mean ftp (and /// mean not ftp!), and where
        #  is file: scheme specified?  I think this is really a bug, and
        #  what was intended was to distinguish between URLs like:
        # file:/blah.txt (a file)
        # file://localhost/blah.txt (a file)
        # file:///blah.txt (a file)
        # file://ftp.example.com/blah.txt (an ftp URL)
        for url, ftp in [
            ("file://ftp.example.com//foo.txt", True),
            ("file://ftp.example.com///foo.txt", False),
                # XXXX bug: fails with OSError, should be URLError
            ("file://ftp.example.com/foo.txt", False),
        ]:
            req = Request(url)
            try:
                h.file_open(req)
            # XXXX remove OSError when bug fixed
            except (urllib.error.URLError, OSError):
                self.assertTrue(not ftp)
            else:
                self.assertTrue(o.req is req)
                self.assertEqual(req.type, "ftp")
Exemplo n.º 20
0
class URLopener(urllib.URLopener):
    def open_http(self, url, data=None):
        """Use HTTP protocol."""
        from eventlib.green import httplib
        user_passwd = None
        proxy_passwd = None
        if isinstance(url, str):
            host, selector = splithost(url)
            if host:
                user_passwd, host = splituser(host)
                host = unquote(host)
            realhost = host
        else:
            host, selector = url
            # check whether the proxy contains authorization information
            proxy_passwd, host = splituser(host)
            # now we proceed with the url we want to obtain
            urltype, rest = splittype(selector)
            url = rest
            user_passwd = None
            if urltype.lower() != 'http':
                realhost = None
            else:
                realhost, rest = splithost(rest)
                if realhost:
                    user_passwd, realhost = splituser(realhost)
                if user_passwd:
                    selector = "%s://%s%s" % (urltype, realhost, rest)
                if proxy_bypass(realhost):
                    host = realhost

            #print "proxy via http:", host, selector
        if not host: raise IOError, ('http error', 'no host given')

        if proxy_passwd:
            import base64
            proxy_auth = base64.b64encode(proxy_passwd).strip()
        else:
            proxy_auth = None

        if user_passwd:
            import base64
            auth = base64.b64encode(user_passwd).strip()
        else:
            auth = None
        h = httplib.HTTP(host)
        if data is not None:
            h.putrequest('POST', selector)
            h.putheader('Content-Type', 'application/x-www-form-urlencoded')
            h.putheader('Content-Length', '%d' % len(data))
        else:
            h.putrequest('GET', selector)
        if proxy_auth:
            h.putheader('Proxy-Authorization', 'Basic %s' % proxy_auth)
        if auth: h.putheader('Authorization', 'Basic %s' % auth)
        if realhost: h.putheader('Host', realhost)
        for args in self.addheaders:
            h.putheader(*args)
        h.endheaders()
        if data is not None:
            h.send(data)
        errcode, errmsg, headers = h.getreply()
        if errcode == -1:
            # something went wrong with the HTTP status line
            raise IOError, ('http protocol error', 0, 'got a bad status line',
                            None)
        fp = h.getfile()
        if errcode == 200:
            return addinfourl(fp, headers, "http:" + url)
        else:
            if data is None:
                return self.http_error(url, fp, errcode, errmsg, headers)
            else:
                return self.http_error(url, fp, errcode, errmsg, headers, data)

    if hasattr(socket, "ssl"):

        def open_https(self, url, data=None):
            """Use HTTPS protocol."""
            from eventlib.green import httplib
            user_passwd = None
            proxy_passwd = None
            if isinstance(url, str):
                host, selector = splithost(url)
                if host:
                    user_passwd, host = splituser(host)
                    host = unquote(host)
                realhost = host
            else:
                host, selector = url
                # here, we determine, whether the proxy contains authorization information
                proxy_passwd, host = splituser(host)
                urltype, rest = splittype(selector)
                url = rest
                user_passwd = None
                if urltype.lower() != 'https':
                    realhost = None
                else:
                    realhost, rest = splithost(rest)
                    if realhost:
                        user_passwd, realhost = splituser(realhost)
                    if user_passwd:
                        selector = "%s://%s%s" % (urltype, realhost, rest)
                #print "proxy via https:", host, selector
            if not host: raise IOError, ('https error', 'no host given')
            if proxy_passwd:
                import base64
                proxy_auth = base64.b64encode(proxy_passwd).strip()
            else:
                proxy_auth = None
            if user_passwd:
                import base64
                auth = base64.b64encode(user_passwd).strip()
            else:
                auth = None
            h = httplib.HTTPS(host,
                              0,
                              key_file=self.key_file,
                              cert_file=self.cert_file)
            if data is not None:
                h.putrequest('POST', selector)
                h.putheader('Content-Type',
                            'application/x-www-form-urlencoded')
                h.putheader('Content-Length', '%d' % len(data))
            else:
                h.putrequest('GET', selector)
            if proxy_auth:
                h.putheader('Proxy-Authorization', 'Basic %s' % proxy_auth)
            if auth: h.putheader('Authorization', 'Basic %s' % auth)
            if realhost: h.putheader('Host', realhost)
            for args in self.addheaders:
                h.putheader(*args)
            h.endheaders()
            if data is not None:
                h.send(data)
            errcode, errmsg, headers = h.getreply()
            if errcode == -1:
                # something went wrong with the HTTP status line
                raise IOError, ('http protocol error', 0,
                                'got a bad status line', None)
            fp = h.getfile()
            if errcode == 200:
                return addinfourl(fp, headers, "https:" + url)
            else:
                if data is None:
                    return self.http_error(url, fp, errcode, errmsg, headers)
                else:
                    return self.http_error(url, fp, errcode, errmsg, headers,
                                           data)

    def open_gopher(self, url):
        """Use Gopher protocol."""
        if not isinstance(url, str):
            raise IOError, (
                'gopher error',
                'proxy support for gopher protocol currently not implemented')
        from eventlib.green import gopherlib
        host, selector = splithost(url)
        if not host: raise IOError, ('gopher error', 'no host given')
        host = unquote(host)
        type, selector = splitgophertype(selector)
        selector, query = splitquery(selector)
        selector = unquote(selector)
        if query:
            query = unquote(query)
            fp = gopherlib.send_query(selector, query, host)
        else:
            fp = gopherlib.send_selector(selector, host)
        return addinfourl(fp, noheaders(), "gopher:" + url)

    def open_local_file(self, url):
        """Use local file."""
        import mimetypes, mimetools, email.Utils
        try:
            from cStringIO import StringIO
        except ImportError:
            from StringIO import StringIO
        host, file = splithost(url)
        localname = url2pathname(file)
        try:
            stats = os.stat(localname)
        except OSError, e:
            raise IOError(e.errno, e.strerror, e.filename)
        size = stats.st_size
        modified = email.Utils.formatdate(stats.st_mtime, usegmt=True)
        mtype = mimetypes.guess_type(url)[0]
        headers = mimetools.Message(
            StringIO(
                'Content-Type: %s\nContent-Length: %d\nLast-modified: %s\n' %
                (mtype or 'text/plain', size, modified)))
        if not host:
            urlfile = file
            if file[:1] == '/':
                urlfile = 'file://' + file
            return addinfourl(open(localname, 'rb'), headers, urlfile)
        host, port = splitport(host)
        if not port \
           and socket.gethostbyname(host) in (localhost(), thishost()):
            urlfile = file
            if file[:1] == '/':
                urlfile = 'file://' + file
            return addinfourl(open(localname, 'rb'), headers, urlfile)
        raise IOError, ('local file error', 'not on local host')