Mercurial > vim
comparison runtime/tools/demoserver.py @ 7788:192ae655ac91 v7.4.1191
commit https://github.com/vim/vim/commit/3b5f929b18492fec291d1ec95a91f54e5912c03b
Author: Bram Moolenaar <Bram@vim.org>
Date: Thu Jan 28 22:37:01 2016 +0100
patch 7.4.1191
Problem: The channel feature isn't working yet.
Solution: Add the connect(), disconnect(), sendexpr() and sendraw()
functions. Add initial documentation. Add a demo server.
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Thu, 28 Jan 2016 22:45:04 +0100 |
parents | |
children | 2313f1a94153 |
comparison
equal
deleted
inserted
replaced
7787:a0ae64e7006f | 7788:192ae655ac91 |
---|---|
1 #!/usr/bin/python | |
2 # Server that will accept connections from a Vim channel. | |
3 # Run this server and then in Vim you can open the channel: | |
4 # :let handle = connect('localhost:8765', 'json') | |
5 # | |
6 # Then Vim can send requests to the server: | |
7 # :let response = sendexpr(handle, 'hello!') | |
8 # | |
9 # And you can control Vim by typing a JSON message here, e.g.: | |
10 # ["ex","echo 'hi there'"] | |
11 # | |
12 # See ":help channel-demo" in Vim. | |
13 | |
14 import SocketServer | |
15 import json | |
16 import socket | |
17 import sys | |
18 import threading | |
19 | |
20 thesocket = None | |
21 | |
22 class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler): | |
23 | |
24 def handle(self): | |
25 print "=== socket opened ===" | |
26 global thesocket | |
27 thesocket = self.request | |
28 while True: | |
29 try: | |
30 data = self.request.recv(4096) | |
31 except socket.error: | |
32 print "=== socket error ===" | |
33 break | |
34 except IOError: | |
35 print "=== socket closed ===" | |
36 break | |
37 if data == '': | |
38 print "=== socket closed ===" | |
39 break | |
40 print "received: {}".format(data) | |
41 try: | |
42 decoded = json.loads(data) | |
43 except ValueError: | |
44 print "json decoding failed" | |
45 decoded = [0, ''] | |
46 | |
47 if decoded[1] == 'hello!': | |
48 response = "got it" | |
49 else: | |
50 response = "what?" | |
51 encoded = json.dumps([decoded[0], response]) | |
52 print "sending {}".format(encoded) | |
53 self.request.sendall(encoded) | |
54 thesocket = None | |
55 | |
56 class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer): | |
57 pass | |
58 | |
59 if __name__ == "__main__": | |
60 HOST, PORT = "localhost", 8765 | |
61 | |
62 server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler) | |
63 ip, port = server.server_address | |
64 | |
65 # Start a thread with the server -- that thread will then start one | |
66 # more thread for each request | |
67 server_thread = threading.Thread(target=server.serve_forever) | |
68 | |
69 # Exit the server thread when the main thread terminates | |
70 server_thread.daemon = True | |
71 server_thread.start() | |
72 print "Server loop running in thread: ", server_thread.name | |
73 | |
74 print "Listening on port {}".format(PORT) | |
75 while True: | |
76 typed = sys.stdin.readline() | |
77 if "quit" in typed: | |
78 print "Goodbye!" | |
79 break | |
80 if thesocket is None: | |
81 print "No socket yet" | |
82 else: | |
83 print "sending {}".format(typed) | |
84 thesocket.sendall(typed) | |
85 | |
86 server.shutdown() | |
87 server.server_close() |