annotate runtime/tools/demoserver.py @ 20788:072ad890c227 v8.2.0946

patch 8.2.0946: cannot use "q" to cancel a number prompt Commit: https://github.com/vim/vim/commit/eebd555733491cb55b9f30fe28772c0fd0ebacf7 Author: Bram Moolenaar <Bram@vim.org> Date: Wed Jun 10 15:45:57 2020 +0200 patch 8.2.0946: cannot use "q" to cancel a number prompt Problem: Cannot use "q" to cancel a number prompt. Solution: Recognize "q" instead of ignoring it.
author Bram Moolenaar <Bram@vim.org>
date Wed, 10 Jun 2020 16:00:05 +0200
parents ed7251c3e2d3
children c725b8e17f1f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1 #!/usr/bin/python
7895
bff95e0d8885 commit https://github.com/vim/vim/commit/f57969a20a4398f56e3028a6cc1102f9f9286ccf
Christian Brabandt <cb@256bit.org>
parents: 7874
diff changeset
2 #
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
3 # Server that will accept connections from a Vim channel.
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
4 # Run this server and then in Vim you can open the channel:
8148
f5da459c5698 commit https://github.com/vim/vim/commit/e0fa3742ead676a3074a10edadbc955e1a89153d
Christian Brabandt <cb@256bit.org>
parents: 7895
diff changeset
5 # :let handle = ch_open('localhost:8765')
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
6 #
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
7 # Then Vim can send requests to the server:
7895
bff95e0d8885 commit https://github.com/vim/vim/commit/f57969a20a4398f56e3028a6cc1102f9f9286ccf
Christian Brabandt <cb@256bit.org>
parents: 7874
diff changeset
8 # :let response = ch_sendexpr(handle, 'hello!')
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
9 #
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
10 # And you can control Vim by typing a JSON message here, e.g.:
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
11 # ["ex","echo 'hi there'"]
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
12 #
7895
bff95e0d8885 commit https://github.com/vim/vim/commit/f57969a20a4398f56e3028a6cc1102f9f9286ccf
Christian Brabandt <cb@256bit.org>
parents: 7874
diff changeset
13 # There is no prompt, just type a line and press Enter.
bff95e0d8885 commit https://github.com/vim/vim/commit/f57969a20a4398f56e3028a6cc1102f9f9286ccf
Christian Brabandt <cb@256bit.org>
parents: 7874
diff changeset
14 # To exit cleanly type "quit<Enter>".
bff95e0d8885 commit https://github.com/vim/vim/commit/f57969a20a4398f56e3028a6cc1102f9f9286ccf
Christian Brabandt <cb@256bit.org>
parents: 7874
diff changeset
15 #
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
16 # See ":help channel-demo" in Vim.
7895
bff95e0d8885 commit https://github.com/vim/vim/commit/f57969a20a4398f56e3028a6cc1102f9f9286ccf
Christian Brabandt <cb@256bit.org>
parents: 7874
diff changeset
17 #
bff95e0d8885 commit https://github.com/vim/vim/commit/f57969a20a4398f56e3028a6cc1102f9f9286ccf
Christian Brabandt <cb@256bit.org>
parents: 7874
diff changeset
18 # This requires Python 2.6 or later.
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
19
7874
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
20 from __future__ import print_function
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
21 import json
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
22 import socket
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
23 import sys
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
24 import threading
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
25
7874
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
26 try:
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
27 # Python 3
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
28 import socketserver
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
29 except ImportError:
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
30 # Python 2
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
31 import SocketServer as socketserver
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
32
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
33 thesocket = None
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
34
7874
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
35 class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
36
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
37 def handle(self):
7874
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
38 print("=== socket opened ===")
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
39 global thesocket
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
40 thesocket = self.request
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
41 while True:
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
42 try:
7874
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
43 data = self.request.recv(4096).decode('utf-8')
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
44 except socket.error:
7874
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
45 print("=== socket error ===")
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
46 break
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
47 except IOError:
7874
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
48 print("=== socket closed ===")
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
49 break
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
50 if data == '':
7874
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
51 print("=== socket closed ===")
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
52 break
8673
ed7251c3e2d3 commit https://github.com/vim/vim/commit/e18c0b39815c5a746887a509c2cd9f11fadaba07
Christian Brabandt <cb@256bit.org>
parents: 8148
diff changeset
53 print("received: {0}".format(data))
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
54 try:
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
55 decoded = json.loads(data)
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
56 except ValueError:
7874
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
57 print("json decoding failed")
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
58 decoded = [-1, '']
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
59
7874
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
60 # Send a response if the sequence number is positive.
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
61 # Negative numbers are used for "eval" responses.
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
62 if decoded[0] >= 0:
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
63 if decoded[1] == 'hello!':
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
64 response = "got it"
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
65 else:
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
66 response = "what?"
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
67 encoded = json.dumps([decoded[0], response])
8673
ed7251c3e2d3 commit https://github.com/vim/vim/commit/e18c0b39815c5a746887a509c2cd9f11fadaba07
Christian Brabandt <cb@256bit.org>
parents: 8148
diff changeset
68 print("sending {0}".format(encoded))
7874
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
69 self.request.sendall(encoded.encode('utf-8'))
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
70 thesocket = None
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
71
7874
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
72 class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
73 pass
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
74
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
75 if __name__ == "__main__":
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
76 HOST, PORT = "localhost", 8765
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
77
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
78 server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
79 ip, port = server.server_address
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
80
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
81 # Start a thread with the server -- that thread will then start one
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
82 # more thread for each request
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
83 server_thread = threading.Thread(target=server.serve_forever)
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
84
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
85 # Exit the server thread when the main thread terminates
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
86 server_thread.daemon = True
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
87 server_thread.start()
7874
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
88 print("Server loop running in thread: ", server_thread.name)
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
89
8673
ed7251c3e2d3 commit https://github.com/vim/vim/commit/e18c0b39815c5a746887a509c2cd9f11fadaba07
Christian Brabandt <cb@256bit.org>
parents: 8148
diff changeset
90 print("Listening on port {0}".format(PORT))
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
91 while True:
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
92 typed = sys.stdin.readline()
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
93 if "quit" in typed:
7874
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
94 print("Goodbye!")
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
95 break
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
96 if thesocket is None:
7874
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
97 print("No socket yet")
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
98 else:
8673
ed7251c3e2d3 commit https://github.com/vim/vim/commit/e18c0b39815c5a746887a509c2cd9f11fadaba07
Christian Brabandt <cb@256bit.org>
parents: 8148
diff changeset
99 print("sending {0}".format(typed))
7874
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
100 thesocket.sendall(typed.encode('utf-8'))
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
101
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
102 server.shutdown()
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
103 server.server_close()