Statistics
| Branch: | Revision:

root / env / lib / python2.7 / site-packages / distribute-0.6.19-py2.7.egg / setuptools / tests / server.py @ 1a305335

History | View | Annotate | Download (1.47 KB)

1
"""Basic http server for tests to simulate PyPI or custom indexes
2
"""
3
import urllib2
4
import sys
5
from threading import Thread
6
from BaseHTTPServer import HTTPServer
7
from SimpleHTTPServer import SimpleHTTPRequestHandler
8

    
9
class IndexServer(HTTPServer):
10
    """Basic single-threaded http server simulating a package index
11

12
    You can use this server in unittest like this::
13
        s = IndexServer()
14
        s.start()
15
        index_url = s.base_url() + 'mytestindex'
16
        # do some test requests to the index
17
        # The index files should be located in setuptools/tests/indexes
18
        s.stop()
19
    """
20
    def __init__(self):
21
        HTTPServer.__init__(self, ('', 0), SimpleHTTPRequestHandler)
22
        self._run = True
23

    
24
    def serve(self):
25
        while True:
26
            self.handle_request()
27
            if not self._run: break
28

    
29
    def start(self):
30
        self.thread = Thread(target=self.serve)
31
        self.thread.start()
32

    
33
    def stop(self):
34
        """self.shutdown is not supported on python < 2.6"""
35
        self._run = False
36
        try:
37
            if sys.version > '2.6':
38
                urllib2.urlopen('http://127.0.0.1:%s/' % self.server_port,
39
                                None, 5)
40
            else:
41
                urllib2.urlopen('http://127.0.0.1:%s/' % self.server_port)
42
        except urllib2.URLError:
43
            pass
44
        self.thread.join()
45

    
46
    def base_url(self):
47
        port = self.server_port
48
        return 'http://127.0.0.1:%s/setuptools/tests/indexes/' % port