Mercurial > vim
view runtime/tools/demoserver.py @ 8591:5bc958a92849 v7.4.1585
commit https://github.com/vim/vim/commit/d22a18928ebcb465393da1418bb88204b97badb1
Author: Bram Moolenaar <Bram@vim.org>
Date: Thu Mar 17 20:50:47 2016 +0100
patch 7.4.1585
Problem: Partial is not recognized everywhere.
Solution: Check for partial in trans_function_name(). (Yasuhiro Matsumoto)
Add a test.
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Thu, 17 Mar 2016 21:00:04 +0100 |
parents | f5da459c5698 |
children | ed7251c3e2d3 |
line wrap: on
line source
#!/usr/bin/python # # Server that will accept connections from a Vim channel. # Run this server and then in Vim you can open the channel: # :let handle = ch_open('localhost:8765') # # Then Vim can send requests to the server: # :let response = ch_sendexpr(handle, 'hello!') # # And you can control Vim by typing a JSON message here, e.g.: # ["ex","echo 'hi there'"] # # There is no prompt, just type a line and press Enter. # To exit cleanly type "quit<Enter>". # # See ":help channel-demo" in Vim. # # This requires Python 2.6 or later. from __future__ import print_function import json import socket import sys import threading try: # Python 3 import socketserver except ImportError: # Python 2 import SocketServer as socketserver thesocket = None class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler): def handle(self): print("=== socket opened ===") global thesocket thesocket = self.request while True: try: data = self.request.recv(4096).decode('utf-8') except socket.error: print("=== socket error ===") break except IOError: print("=== socket closed ===") break if data == '': print("=== socket closed ===") break print("received: {}".format(data)) try: decoded = json.loads(data) except ValueError: print("json decoding failed") decoded = [-1, ''] # Send a response if the sequence number is positive. # Negative numbers are used for "eval" responses. if decoded[0] >= 0: if decoded[1] == 'hello!': response = "got it" else: response = "what?" encoded = json.dumps([decoded[0], response]) print("sending {}".format(encoded)) self.request.sendall(encoded.encode('utf-8')) thesocket = None class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer): pass if __name__ == "__main__": HOST, PORT = "localhost", 8765 server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler) ip, port = server.server_address # Start a thread with the server -- that thread will then start one # more thread for each request server_thread = threading.Thread(target=server.serve_forever) # Exit the server thread when the main thread terminates server_thread.daemon = True server_thread.start() print("Server loop running in thread: ", server_thread.name) print("Listening on port {}".format(PORT)) while True: typed = sys.stdin.readline() if "quit" in typed: print("Goodbye!") break if thesocket is None: print("No socket yet") else: print("sending {}".format(typed)) thesocket.sendall(typed.encode('utf-8')) server.shutdown() server.server_close()