LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Server that will accept connections from a Vim channel. |
| 4 | # Used by test_channel.vim. |
| 5 | # |
| 6 | # This requires Python 2.6 or later. |
| 7 | |
| 8 | from __future__ import print_function |
LemonBoy | 1b76a8d | 2022-04-04 21:13:35 +0100 | [diff] [blame] | 9 | from test_channel import ThreadedTCPServer, TestingRequestHandler, \ |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 10 | writePortInFile |
| 11 | import socket |
| 12 | import threading |
| 13 | import os |
| 14 | |
| 15 | try: |
| 16 | FileNotFoundError |
| 17 | except NameError: |
| 18 | # Python 2 |
| 19 | FileNotFoundError = (IOError, OSError) |
| 20 | |
LemonBoy | 1b76a8d | 2022-04-04 21:13:35 +0100 | [diff] [blame] | 21 | if not hasattr(socket, "AF_UNIX"): |
| 22 | raise NotImplementedError("Unix sockets are not supported on this platform") |
| 23 | |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 24 | class ThreadedUnixServer(ThreadedTCPServer): |
| 25 | address_family = socket.AF_UNIX |
| 26 | |
LemonBoy | 1b76a8d | 2022-04-04 21:13:35 +0100 | [diff] [blame] | 27 | class ThreadedUnixRequestHandler(TestingRequestHandler): |
| 28 | pass |
| 29 | |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 30 | def main(path): |
LemonBoy | 1b76a8d | 2022-04-04 21:13:35 +0100 | [diff] [blame] | 31 | server = ThreadedUnixServer(path, ThreadedUnixRequestHandler) |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 32 | |
| 33 | # Start a thread with the server. That thread will then start a new thread |
| 34 | # for each connection. |
| 35 | server_thread = threading.Thread(target=server.serve_forever) |
| 36 | server_thread.start() |
| 37 | |
| 38 | # Signal the test harness we're ready, the port value has no meaning. |
| 39 | writePortInFile(1234) |
| 40 | |
| 41 | print("Listening on {0}".format(server.server_address)) |
| 42 | |
| 43 | # Main thread terminates, but the server continues running |
| 44 | # until server.shutdown() is called. |
| 45 | try: |
| 46 | while server_thread.is_alive(): |
| 47 | server_thread.join(1) |
| 48 | except (KeyboardInterrupt, SystemExit): |
| 49 | server.shutdown() |
| 50 | |
| 51 | if __name__ == "__main__": |
| 52 | try: |
| 53 | os.remove("Xtestsocket") |
| 54 | except FileNotFoundError: |
| 55 | pass |
| 56 | main("Xtestsocket") |