# HG changeset patch # User Matt Mackall # Date 2006-11-13 19:26:57 # Node ID dc3504af7722a6c52f4d33a78093215f3021af6c # Parent 44e75d9fa654c3c8f490df3d9bb4d9432a2cd39f hgweb: internalize some socket details diff --git a/mercurial/commands.py b/mercurial/commands.py --- a/mercurial/commands.py +++ b/mercurial/commands.py @@ -11,7 +11,7 @@ from i18n import gettext as _ demandload(globals(), "os re sys signal shutil imp urllib pdb shlex") demandload(globals(), "fancyopts ui hg util lock revlog templater bundlerepo") demandload(globals(), "difflib patch tempfile time") -demandload(globals(), "traceback errno socket version atexit sets bz2") +demandload(globals(), "traceback errno version atexit sets bz2") demandload(globals(), "archival changegroup cmdutil hgweb.server sshserver") class UnknownCommand(Exception): @@ -2531,24 +2531,14 @@ def serve(ui, repo, **opts): os.read(rfd, 1) os._exit(0) - try: - httpd = hgweb.server.create_server(ui, repo) - except socket.error, inst: - raise util.Abort(_('cannot start server: %s') % inst.args[1]) + httpd = hgweb.server.create_server(ui, repo) if ui.verbose: - addr, port = httpd.socket.getsockname() - if addr == '0.0.0.0': - addr = socket.gethostname() + if httpd.port != 80: + ui.status(_('listening at http://%s:%d/\n') % + (httpd.addr, httpd.port)) else: - try: - addr = socket.gethostbyaddr(addr)[0] - except socket.error: - pass - if port != 80: - ui.status(_('listening at http://%s:%d/\n') % (addr, port)) - else: - ui.status(_('listening at http://%s/\n') % addr) + ui.status(_('listening at http://%s/\n') % httpd.addr) if opts['pid_file']: fp = open(opts['pid_file'], 'w') diff --git a/mercurial/hgweb/server.py b/mercurial/hgweb/server.py --- a/mercurial/hgweb/server.py +++ b/mercurial/hgweb/server.py @@ -200,6 +200,16 @@ def create_server(ui, repo): self.reqmaker = wsgiapplication(self.make_handler) self.daemon_threads = True + addr, port = self.socket.getsockname() + if addr == '0.0.0.0': + addr = socket.gethostname() + else: + try: + addr = socket.gethostbyaddr(addr)[0] + except socket.error: + pass + self.addr, self.port = addr, port + def make_handler(self): if self.webdir_conf: hgwebobj = self.webdirmaker(self.webdir_conf) @@ -219,7 +229,10 @@ def create_server(ui, repo): raise hg.RepoError(_('IPv6 not available on this system')) super(IPv6HTTPServer, self).__init__(*args, **kwargs) - if use_ipv6: - return IPv6HTTPServer((address, port), _hgwebhandler) - else: - return MercurialHTTPServer((address, port), _hgwebhandler) + try: + if use_ipv6: + return IPv6HTTPServer((address, port), _hgwebhandler) + else: + return MercurialHTTPServer((address, port), _hgwebhandler) + except socket.error, inst: + raise util.Abort(_('cannot start server: %s') % inst.args[1])