擅长:python、mysql、java
<p>另一种解决方案是在再次打开之前使用@LuisMuñoz的评论:<a href="https://stackoverflow.com/a/19196218/1422096">check if the port is already opened</a>:</p>
<pre><code># Bottle web server code here
# ...
import socket
sock = socket.socket()
sock.settimeout(0.2) # this prevents a 2 second lag when starting the server
if sock.connect_ex(('127.0.0.1', 80)) == 0:
print "Sorry, port already in use."
exit()
run(host='0.0.0.0', port=80)
</code></pre>