Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 2 | # |
| 3 | # Server that will accept connections from a Vim channel. |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 4 | # Used by test_channel.vim. |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 5 | # |
| 6 | # This requires Python 2.6 or later. |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | import json |
| 10 | import socket |
| 11 | import sys |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 12 | import time |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 13 | import threading |
| 14 | |
| 15 | try: |
| 16 | # Python 3 |
| 17 | import socketserver |
| 18 | except ImportError: |
| 19 | # Python 2 |
| 20 | import SocketServer as socketserver |
| 21 | |
LemonBoy | 1b76a8d | 2022-04-04 21:13:35 +0100 | [diff] [blame] | 22 | class TestingRequestHandler(socketserver.BaseRequestHandler): |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 23 | def handle(self): |
| 24 | print("=== socket opened ===") |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 25 | while True: |
| 26 | try: |
Bram Moolenaar | 608a891 | 2016-02-03 22:39:51 +0100 | [diff] [blame] | 27 | received = self.request.recv(4096).decode('utf-8') |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 28 | except socket.error: |
| 29 | print("=== socket error ===") |
| 30 | break |
| 31 | except IOError: |
| 32 | print("=== socket closed ===") |
| 33 | break |
Bram Moolenaar | 608a891 | 2016-02-03 22:39:51 +0100 | [diff] [blame] | 34 | if received == '': |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 35 | print("=== socket closed ===") |
| 36 | break |
Bram Moolenaar | a63cdb5 | 2016-03-20 18:24:45 +0100 | [diff] [blame] | 37 | print("received: {0}".format(received)) |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 38 | |
Bram Moolenaar | e7bed62 | 2016-02-03 22:20:29 +0100 | [diff] [blame] | 39 | # We may receive two messages at once. Take the part up to the |
Bram Moolenaar | f1f0792 | 2016-08-26 17:58:53 +0200 | [diff] [blame] | 40 | # newline, which should be after the matching "]". |
Bram Moolenaar | 608a891 | 2016-02-03 22:39:51 +0100 | [diff] [blame] | 41 | todo = received |
| 42 | while todo != '': |
Bram Moolenaar | f1f0792 | 2016-08-26 17:58:53 +0200 | [diff] [blame] | 43 | splitidx = todo.find('\n') |
Bram Moolenaar | e7bed62 | 2016-02-03 22:20:29 +0100 | [diff] [blame] | 44 | if splitidx < 0: |
Bram Moolenaar | 608a891 | 2016-02-03 22:39:51 +0100 | [diff] [blame] | 45 | used = todo |
| 46 | todo = '' |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 47 | else: |
Bram Moolenaar | f1f0792 | 2016-08-26 17:58:53 +0200 | [diff] [blame] | 48 | used = todo[:splitidx] |
Bram Moolenaar | 608a891 | 2016-02-03 22:39:51 +0100 | [diff] [blame] | 49 | todo = todo[splitidx + 1:] |
| 50 | if used != received: |
Bram Moolenaar | a63cdb5 | 2016-03-20 18:24:45 +0100 | [diff] [blame] | 51 | print("using: {0}".format(used)) |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 52 | |
Bram Moolenaar | e7bed62 | 2016-02-03 22:20:29 +0100 | [diff] [blame] | 53 | try: |
Bram Moolenaar | 608a891 | 2016-02-03 22:39:51 +0100 | [diff] [blame] | 54 | decoded = json.loads(used) |
Bram Moolenaar | e7bed62 | 2016-02-03 22:20:29 +0100 | [diff] [blame] | 55 | except ValueError: |
| 56 | print("json decoding failed") |
| 57 | decoded = [-1, ''] |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 58 | |
Bram Moolenaar | e7bed62 | 2016-02-03 22:20:29 +0100 | [diff] [blame] | 59 | # Send a response if the sequence number is positive. |
| 60 | if decoded[0] >= 0: |
| 61 | if decoded[1] == 'hello!': |
| 62 | # simply send back a string |
| 63 | response = "got it" |
Bram Moolenaar | 5d7ead3 | 2018-02-27 17:17:42 +0100 | [diff] [blame] | 64 | elif decoded[1] == 'malformed1': |
| 65 | cmd = '["ex",":"]wrong!["ex","smi"]' |
| 66 | print("sending: {0}".format(cmd)) |
| 67 | self.request.sendall(cmd.encode('utf-8')) |
| 68 | response = "ok" |
| 69 | # Need to wait for Vim to give up, otherwise it |
| 70 | # sometimes fails on OS X. |
| 71 | time.sleep(0.2) |
| 72 | elif decoded[1] == 'malformed2': |
| 73 | cmd = '"unterminated string' |
| 74 | print("sending: {0}".format(cmd)) |
| 75 | self.request.sendall(cmd.encode('utf-8')) |
| 76 | response = "ok" |
| 77 | # Need to wait for Vim to give up, otherwise the double |
| 78 | # quote in the "ok" response terminates the string. |
| 79 | time.sleep(0.2) |
| 80 | elif decoded[1] == 'malformed3': |
| 81 | cmd = '["ex","missing ]"' |
| 82 | print("sending: {0}".format(cmd)) |
| 83 | self.request.sendall(cmd.encode('utf-8')) |
| 84 | response = "ok" |
| 85 | # Need to wait for Vim to give up, otherwise the ] |
| 86 | # in the "ok" response terminates the list. |
| 87 | time.sleep(0.2) |
| 88 | elif decoded[1] == 'split': |
| 89 | cmd = '["ex","let ' |
| 90 | print("sending: {0}".format(cmd)) |
| 91 | self.request.sendall(cmd.encode('utf-8')) |
| 92 | time.sleep(0.01) |
| 93 | cmd = 'g:split = 123"]' |
| 94 | print("sending: {0}".format(cmd)) |
| 95 | self.request.sendall(cmd.encode('utf-8')) |
| 96 | response = "ok" |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 97 | elif decoded[1].startswith("echo "): |
| 98 | # send back the argument |
| 99 | response = decoded[1][5:] |
Bram Moolenaar | e7bed62 | 2016-02-03 22:20:29 +0100 | [diff] [blame] | 100 | elif decoded[1] == 'make change': |
Bram Moolenaar | 66624ff | 2016-02-03 23:59:43 +0100 | [diff] [blame] | 101 | # Send two ex commands at the same time, before |
| 102 | # replying to the request. |
Bram Moolenaar | e7bed62 | 2016-02-03 22:20:29 +0100 | [diff] [blame] | 103 | cmd = '["ex","call append(\\"$\\",\\"added1\\")"]' |
| 104 | cmd += '["ex","call append(\\"$\\",\\"added2\\")"]' |
Bram Moolenaar | a63cdb5 | 2016-03-20 18:24:45 +0100 | [diff] [blame] | 105 | print("sending: {0}".format(cmd)) |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 106 | self.request.sendall(cmd.encode('utf-8')) |
Bram Moolenaar | e7bed62 | 2016-02-03 22:20:29 +0100 | [diff] [blame] | 107 | response = "ok" |
Bram Moolenaar | b836f63 | 2021-07-01 22:11:28 +0200 | [diff] [blame] | 108 | elif decoded[1] == 'echoerr': |
| 109 | cmd = '["ex","echoerr \\\"this is an error\\\""]' |
| 110 | print("sending: {0}".format(cmd)) |
| 111 | self.request.sendall(cmd.encode('utf-8')) |
| 112 | response = "ok" |
Bram Moolenaar | 890ee4e | 2021-07-30 21:56:10 +0200 | [diff] [blame] | 113 | # Wait a bit, so that the "ex" command is handled |
| 114 | # before the "ch_evalexpr() returns. Otherwise we are |
| 115 | # outside the try/catch when the "ex" command is |
| 116 | # handled. |
| 117 | time.sleep(0.02) |
Bram Moolenaar | c4dcd60 | 2016-03-26 22:56:46 +0100 | [diff] [blame] | 118 | elif decoded[1] == 'bad command': |
| 119 | cmd = '["ex","foo bar"]' |
| 120 | print("sending: {0}".format(cmd)) |
| 121 | self.request.sendall(cmd.encode('utf-8')) |
| 122 | response = "ok" |
Bram Moolenaar | f416086 | 2016-02-05 23:09:12 +0100 | [diff] [blame] | 123 | elif decoded[1] == 'do normal': |
| 124 | # Send a normal command. |
| 125 | cmd = '["normal","G$s more\u001b"]' |
Bram Moolenaar | a63cdb5 | 2016-03-20 18:24:45 +0100 | [diff] [blame] | 126 | print("sending: {0}".format(cmd)) |
Bram Moolenaar | f416086 | 2016-02-05 23:09:12 +0100 | [diff] [blame] | 127 | self.request.sendall(cmd.encode('utf-8')) |
| 128 | response = "ok" |
Bram Moolenaar | e7bed62 | 2016-02-03 22:20:29 +0100 | [diff] [blame] | 129 | elif decoded[1] == 'eval-works': |
| 130 | # Send an eval request. We ignore the response. |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 131 | cmd = '["expr","\\"foo\\" . 123", -1]' |
Bram Moolenaar | a63cdb5 | 2016-03-20 18:24:45 +0100 | [diff] [blame] | 132 | print("sending: {0}".format(cmd)) |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 133 | self.request.sendall(cmd.encode('utf-8')) |
Bram Moolenaar | e7bed62 | 2016-02-03 22:20:29 +0100 | [diff] [blame] | 134 | response = "ok" |
Bram Moolenaar | fa8b2e1 | 2016-03-26 22:19:27 +0100 | [diff] [blame] | 135 | elif decoded[1] == 'eval-special': |
| 136 | # Send an eval request. We ignore the response. |
| 137 | cmd = '["expr","\\"foo\x7f\x10\x01bar\\"", -2]' |
| 138 | print("sending: {0}".format(cmd)) |
| 139 | self.request.sendall(cmd.encode('utf-8')) |
| 140 | response = "ok" |
| 141 | elif decoded[1] == 'eval-getline': |
| 142 | # Send an eval request. We ignore the response. |
| 143 | cmd = '["expr","getline(3)", -3]' |
| 144 | print("sending: {0}".format(cmd)) |
| 145 | self.request.sendall(cmd.encode('utf-8')) |
| 146 | response = "ok" |
Bram Moolenaar | e7bed62 | 2016-02-03 22:20:29 +0100 | [diff] [blame] | 147 | elif decoded[1] == 'eval-fails': |
| 148 | # Send an eval request that will fail. |
Bram Moolenaar | fa8b2e1 | 2016-03-26 22:19:27 +0100 | [diff] [blame] | 149 | cmd = '["expr","xxx", -4]' |
Bram Moolenaar | a63cdb5 | 2016-03-20 18:24:45 +0100 | [diff] [blame] | 150 | print("sending: {0}".format(cmd)) |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 151 | self.request.sendall(cmd.encode('utf-8')) |
Bram Moolenaar | e7bed62 | 2016-02-03 22:20:29 +0100 | [diff] [blame] | 152 | response = "ok" |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 153 | elif decoded[1] == 'eval-error': |
| 154 | # Send an eval request that works but the result can't |
| 155 | # be encoded. |
Bram Moolenaar | fa8b2e1 | 2016-03-26 22:19:27 +0100 | [diff] [blame] | 156 | cmd = '["expr","function(\\"tr\\")", -5]' |
Bram Moolenaar | a63cdb5 | 2016-03-20 18:24:45 +0100 | [diff] [blame] | 157 | print("sending: {0}".format(cmd)) |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 158 | self.request.sendall(cmd.encode('utf-8')) |
| 159 | response = "ok" |
Bram Moolenaar | 66624ff | 2016-02-03 23:59:43 +0100 | [diff] [blame] | 160 | elif decoded[1] == 'eval-bad': |
| 161 | # Send an eval request missing the third argument. |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 162 | cmd = '["expr","xxx"]' |
Bram Moolenaar | a63cdb5 | 2016-03-20 18:24:45 +0100 | [diff] [blame] | 163 | print("sending: {0}".format(cmd)) |
Bram Moolenaar | 66624ff | 2016-02-03 23:59:43 +0100 | [diff] [blame] | 164 | self.request.sendall(cmd.encode('utf-8')) |
| 165 | response = "ok" |
Bram Moolenaar | f416086 | 2016-02-05 23:09:12 +0100 | [diff] [blame] | 166 | elif decoded[1] == 'an expr': |
| 167 | # Send an expr request. |
| 168 | cmd = '["expr","setline(\\"$\\", [\\"one\\",\\"two\\",\\"three\\"])"]' |
Bram Moolenaar | a63cdb5 | 2016-03-20 18:24:45 +0100 | [diff] [blame] | 169 | print("sending: {0}".format(cmd)) |
Bram Moolenaar | f416086 | 2016-02-05 23:09:12 +0100 | [diff] [blame] | 170 | self.request.sendall(cmd.encode('utf-8')) |
| 171 | response = "ok" |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 172 | elif decoded[1] == 'call-func': |
| 173 | cmd = '["call","MyFunction",[1,2,3], 0]' |
Bram Moolenaar | a63cdb5 | 2016-03-20 18:24:45 +0100 | [diff] [blame] | 174 | print("sending: {0}".format(cmd)) |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 175 | self.request.sendall(cmd.encode('utf-8')) |
| 176 | response = "ok" |
Bram Moolenaar | f416086 | 2016-02-05 23:09:12 +0100 | [diff] [blame] | 177 | elif decoded[1] == 'redraw': |
| 178 | cmd = '["redraw",""]' |
Bram Moolenaar | a63cdb5 | 2016-03-20 18:24:45 +0100 | [diff] [blame] | 179 | print("sending: {0}".format(cmd)) |
Bram Moolenaar | f416086 | 2016-02-05 23:09:12 +0100 | [diff] [blame] | 180 | self.request.sendall(cmd.encode('utf-8')) |
| 181 | response = "ok" |
| 182 | elif decoded[1] == 'redraw!': |
| 183 | cmd = '["redraw","force"]' |
Bram Moolenaar | a63cdb5 | 2016-03-20 18:24:45 +0100 | [diff] [blame] | 184 | print("sending: {0}".format(cmd)) |
Bram Moolenaar | f416086 | 2016-02-05 23:09:12 +0100 | [diff] [blame] | 185 | self.request.sendall(cmd.encode('utf-8')) |
| 186 | response = "ok" |
Bram Moolenaar | 6076fe1 | 2016-02-05 22:49:56 +0100 | [diff] [blame] | 187 | elif decoded[1] == 'empty-request': |
| 188 | cmd = '[]' |
Bram Moolenaar | a63cdb5 | 2016-03-20 18:24:45 +0100 | [diff] [blame] | 189 | print("sending: {0}".format(cmd)) |
Bram Moolenaar | 6076fe1 | 2016-02-05 22:49:56 +0100 | [diff] [blame] | 190 | self.request.sendall(cmd.encode('utf-8')) |
| 191 | response = "ok" |
Bram Moolenaar | e7bed62 | 2016-02-03 22:20:29 +0100 | [diff] [blame] | 192 | elif decoded[1] == 'eval-result': |
| 193 | # Send back the last received eval result. |
| 194 | response = last_eval |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 195 | elif decoded[1] == 'call me': |
| 196 | cmd = '[0,"we called you"]' |
Bram Moolenaar | a63cdb5 | 2016-03-20 18:24:45 +0100 | [diff] [blame] | 197 | print("sending: {0}".format(cmd)) |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 198 | self.request.sendall(cmd.encode('utf-8')) |
| 199 | response = "ok" |
| 200 | elif decoded[1] == 'call me again': |
| 201 | cmd = '[0,"we did call you"]' |
Bram Moolenaar | a63cdb5 | 2016-03-20 18:24:45 +0100 | [diff] [blame] | 202 | print("sending: {0}".format(cmd)) |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 203 | self.request.sendall(cmd.encode('utf-8')) |
| 204 | response = "" |
Bram Moolenaar | 5983ad0 | 2016-03-05 20:54:36 +0100 | [diff] [blame] | 205 | elif decoded[1] == 'send zero': |
| 206 | cmd = '[0,"zero index"]' |
Bram Moolenaar | a63cdb5 | 2016-03-20 18:24:45 +0100 | [diff] [blame] | 207 | print("sending: {0}".format(cmd)) |
Bram Moolenaar | 5983ad0 | 2016-03-05 20:54:36 +0100 | [diff] [blame] | 208 | self.request.sendall(cmd.encode('utf-8')) |
| 209 | response = "sent zero" |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 210 | elif decoded[1] == 'close me': |
| 211 | print("closing") |
| 212 | self.request.close() |
| 213 | response = "" |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 214 | elif decoded[1] == 'wait a bit': |
| 215 | time.sleep(0.2) |
| 216 | response = "waited" |
Bram Moolenaar | e7bed62 | 2016-02-03 22:20:29 +0100 | [diff] [blame] | 217 | elif decoded[1] == '!quit!': |
| 218 | # we're done |
Bram Moolenaar | b3e2f00 | 2016-02-04 00:11:37 +0100 | [diff] [blame] | 219 | self.server.shutdown() |
Bram Moolenaar | b92abad | 2016-02-08 22:37:24 +0100 | [diff] [blame] | 220 | return |
Bram Moolenaar | e7bed62 | 2016-02-03 22:20:29 +0100 | [diff] [blame] | 221 | elif decoded[1] == '!crash!': |
| 222 | # Crash! |
| 223 | 42 / 0 |
| 224 | else: |
| 225 | response = "what?" |
| 226 | |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 227 | if response == "": |
| 228 | print("no response") |
| 229 | else: |
| 230 | encoded = json.dumps([decoded[0], response]) |
Bram Moolenaar | a63cdb5 | 2016-03-20 18:24:45 +0100 | [diff] [blame] | 231 | print("sending: {0}".format(encoded)) |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 232 | self.request.sendall(encoded.encode('utf-8')) |
Bram Moolenaar | e7bed62 | 2016-02-03 22:20:29 +0100 | [diff] [blame] | 233 | |
| 234 | # Negative numbers are used for "eval" responses. |
| 235 | elif decoded[0] < 0: |
| 236 | last_eval = decoded |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 237 | |
LemonBoy | 1b76a8d | 2022-04-04 21:13:35 +0100 | [diff] [blame] | 238 | class ThreadedTCPRequestHandler(TestingRequestHandler): |
| 239 | def setup(self): |
| 240 | self.request.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) |
| 241 | |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 242 | class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer): |
| 243 | pass |
| 244 | |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 245 | def writePortInFile(port): |
| 246 | # Write the port number in Xportnr, so that the test knows it. |
| 247 | f = open("Xportnr", "w") |
Bram Moolenaar | a63cdb5 | 2016-03-20 18:24:45 +0100 | [diff] [blame] | 248 | f.write("{0}".format(port)) |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 249 | f.close() |
| 250 | |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 251 | def main(host, port, server_class=ThreadedTCPServer): |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 252 | # Wait half a second before opening the port to test waittime in ch_open(). |
| 253 | # We do want to get the port number, get that first. We cannot open the |
| 254 | # socket, guess a port is free. |
| 255 | if len(sys.argv) >= 2 and sys.argv[1] == 'delay': |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 256 | port = 13684 |
| 257 | writePortInFile(port) |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 258 | |
| 259 | print("Wait for it...") |
| 260 | time.sleep(0.5) |
| 261 | |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 262 | server = server_class((host, port), ThreadedTCPRequestHandler) |
| 263 | ip, port = server.server_address[0:2] |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 264 | |
Bram Moolenaar | 6076fe1 | 2016-02-05 22:49:56 +0100 | [diff] [blame] | 265 | # Start a thread with the server. That thread will then start a new thread |
| 266 | # for each connection. |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 267 | server_thread = threading.Thread(target=server.serve_forever) |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 268 | server_thread.start() |
| 269 | |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 270 | writePortInFile(port) |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 271 | |
Bram Moolenaar | a63cdb5 | 2016-03-20 18:24:45 +0100 | [diff] [blame] | 272 | print("Listening on port {0}".format(port)) |
Bram Moolenaar | b3e2f00 | 2016-02-04 00:11:37 +0100 | [diff] [blame] | 273 | |
| 274 | # Main thread terminates, but the server continues running |
| 275 | # until server.shutdown() is called. |
Bram Moolenaar | ddbe7d2 | 2016-02-20 18:26:48 +0100 | [diff] [blame] | 276 | try: |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 277 | while server_thread.is_alive(): |
Bram Moolenaar | ddbe7d2 | 2016-02-20 18:26:48 +0100 | [diff] [blame] | 278 | server_thread.join(1) |
| 279 | except (KeyboardInterrupt, SystemExit): |
| 280 | server.shutdown() |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 281 | |
| 282 | if __name__ == "__main__": |
| 283 | main("localhost", 0) |