我已经看到了其他有关此问题,但没有一个解决方案适合我。我在MacOS上运行Python 2.7.10。
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
import unittest, urllib2
from thread import start_new_thread
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write("Hello")
class TestMyServer(unittest.TestCase):
def setUp(self):
self.server = HTTPServer(('127.0.0.1', 8080), Handler)
start_new_thread(self.server.serve_forever, ())
def tearDown(self):
self.server.shutdown()
def test1(self):
resp = urllib2.urlopen("http://localhost:8080")
print resp.read()
def test2(self):
resp = urllib2.urlopen("http://localhost:8080")
print resp.read()
if __name__ == "__main__":
unittest.main()
代码给出:
error: [Errno 48] Address already in use
运行第二次测试时。我该如何解决这个问题?