1

I'm currently learning CherryPy, usnig Python 2.7 on PyCharm, on Windows 10. While I executed a few programs on it, I noticed something. The first time I run the program it successfully executes, and the required o/p appears on localhost port 8080 in the browser. However, if I change something in the same program and run it again, I get this:

C:\Python27\python.exe C:/Users/ymodak/Desktop/Training/x.py
[03/Jul/2018:11:15:37] ENGINE Listening for SIGTERM.
[03/Jul/2018:11:15:37] ENGINE Bus STARTING
CherryPy Checker:
The Application mounted at '' has an empty config.

[03/Jul/2018:11:15:37] ENGINE Set handler for console events.
[03/Jul/2018:11:15:37] ENGINE Started monitor thread 'Autoreloader'.
[03/Jul/2018:11:15:39] ENGINE Error in 'start' listener <bound method             
Server.start of <cherrypy._cpserver.Server object at 0x0000000003B14F98>>
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\cherrypy\process\wspbus.py", line 230, in publish
output.append(listener(*args, **kwargs))
  File "C:\Python27\lib\site-packages\cherrypy\_cpserver.py", line 191, in start
super(Server, self).start()
  File "C:\Python27\lib\site-packages\cherrypy\process\servers.py", line 177, in start
portend.free(*self.bind_addr, timeout=Timeouts.free)
  File "C:\Python27\lib\site-packages\portend.py", line 119, in free
raise Timeout("Port {port} not free on {host}.".format(**locals()))
Timeout: Port 8080 not free on 127.0.0.1.

[03/Jul/2018:11:15:39] ENGINE Shutting down due to error in start listener:
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\cherrypy\process\wspbus.py", line 268, in start
self.publish('start')
  File "C:\Python27\lib\site-packages\cherrypy\process\wspbus.py", line 248, in publish
raise exc
ChannelFailures: Timeout('Port 8080 not free on 127.0.0.1.',)

[03/Jul/2018:11:15:39] ENGINE Bus STOPPING
[03/Jul/2018:11:15:39] ENGINE HTTP Server         
cherrypy._cpwsgi_server.CPWSGIServer(('127.0.0.1', 8080)) already shut down
[03/Jul/2018:11:15:39] ENGINE Stopped thread 'Autoreloader'.
[03/Jul/2018:11:15:39] ENGINE Removed handler for console events.
[03/Jul/2018:11:15:39] ENGINE Bus STOPPED
[03/Jul/2018:11:15:39] ENGINE Bus EXITING
[03/Jul/2018:11:15:39] ENGINE Bus EXITED

Process finished with exit code 70

I have already looked at:

1.How to kill a currently using port on localhost in windows?

2.Error: That port is already in use.

But what I want to know is a way to shut the port immediately after I stop the execution of the program, and close the browser. Which is to say, I want the port to close automatically when the program executed on it is shut down. Is there any way to do that? Any help is appreciated. Thanks a lot in advance.

P.S: This is the program I'm running:

import random
import string

import cherrypy


class StringGenerator(object):
    @cherrypy.expose
    def index(self):
        return """<html>
      <head></head>
      <body>
        <form method="put" action="generate">
          <input type="text" value="8" name="length" />
          <button type="submit">Generate!</button>
        </form>
      </body>
    </html>"""

    @cherrypy.expose
    def generate(self, length=8):
        return ''.join(random.sample(string.hexdigits, int(length)))


if __name__ == '__main__':
    cherrypy.quickstart(StringGenerator())

1 Answers1

1

The port is probably in use because the program is still running - the port is in use by the previous invocation of your program.

Make sure you fully exit the program before restarting it.

PyCharm has a feature - Single Instance Only - that should do this for you. I'd recommend giving that a try.

Shadow
  • 6,976
  • 4
  • 39
  • 52