annotate runtime/tools/demoserver.py @ 7874:2313f1a94153 v7.4.1234

commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23 Author: Bram Moolenaar <Bram@vim.org> Date: Mon Feb 1 22:01:10 2016 +0100 patch 7.4.1234 Problem: Demo server only runs with Python 2. Solution: Make it run with Python 3 as well. (Ken Takata)
author Christian Brabandt <cb@256bit.org>
date Mon, 01 Feb 2016 22:15:04 +0100
parents 192ae655ac91
children bff95e0d8885
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
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
2 # 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
3 # Run this server and then in Vim you can open the channel:
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
4 # :let handle = connect('localhost:8765', 'json')
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
5 #
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
6 # Then Vim can send requests to the server:
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
7 # :let response = sendexpr(handle, 'hello!')
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
8 #
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
9 # 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
10 # ["ex","echo 'hi there'"]
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
11 #
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
12 # See ":help channel-demo" in Vim.
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
13
7874
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
14 from __future__ import print_function
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
15 import json
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
16 import socket
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
17 import sys
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
18 import threading
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 try:
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
21 # Python 3
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
22 import socketserver
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
23 except ImportError:
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
24 # Python 2
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
25 import SocketServer as socketserver
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
26
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
27 thesocket = None
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
28
7874
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
29 class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
30
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
31 def handle(self):
7874
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
32 print("=== socket opened ===")
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
33 global thesocket
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
34 thesocket = self.request
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
35 while True:
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
36 try:
7874
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
37 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
38 except socket.error:
7874
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
39 print("=== socket error ===")
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
40 break
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
41 except IOError:
7874
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
42 print("=== socket closed ===")
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
43 break
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
44 if data == '':
7874
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
45 print("=== socket closed ===")
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
46 break
7874
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
47 print("received: {}".format(data))
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
48 try:
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
49 decoded = json.loads(data)
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
50 except ValueError:
7874
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
51 print("json decoding failed")
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
52 decoded = [-1, '']
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
53
7874
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
54 # 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
55 # 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
56 if decoded[0] >= 0:
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
57 if decoded[1] == 'hello!':
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
58 response = "got it"
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
59 else:
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
60 response = "what?"
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
61 encoded = json.dumps([decoded[0], response])
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
62 print("sending {}".format(encoded))
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
63 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
64 thesocket = None
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
65
7874
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
66 class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
67 pass
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
68
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
69 if __name__ == "__main__":
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
70 HOST, PORT = "localhost", 8765
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
71
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
72 server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
73 ip, port = server.server_address
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 # 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
76 # more thread for each request
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
77 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
78
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
79 # 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
80 server_thread.daemon = True
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
81 server_thread.start()
7874
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
82 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
83
7874
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
84 print("Listening on port {}".format(PORT))
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
85 while True:
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
86 typed = sys.stdin.readline()
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
87 if "quit" in typed:
7874
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
88 print("Goodbye!")
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
89 break
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
90 if thesocket is None:
7874
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
91 print("No socket yet")
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
92 else:
7874
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
93 print("sending {}".format(typed))
2313f1a94153 commit https://github.com/vim/vim/commit/488a130ea261f02317adc2c2ca93cc6e68cf2c23
Christian Brabandt <cb@256bit.org>
parents: 7788
diff changeset
94 thesocket.sendall(typed.encode('utf-8'))
7788
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
95
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
96 server.shutdown()
192ae655ac91 commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
97 server.server_close()