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 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.º 3
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.º 4
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.º 5
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.º 6
0
def thishost():
    """Return the IP address of the current host."""
    global _thishost
    if _thishost is None:
        _thishost = socket.gethostbyname(socket.gethostname())
    return _thishost