Hi,
Currently, the runtime/tools/demoserver.py cannot be executed with Python 3.
Attached patch enables demoserver.py to be executed with both Python 2 and 3.
Regards,
Ken Takata
--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
---
You received this message because you are subscribed to the Google Groups
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.
# HG changeset patch
# Parent 1d1a3bb1529762f017de4d8d9ecc9a0c45da018c
diff --git a/runtime/tools/demoserver.py b/runtime/tools/demoserver.py
--- a/runtime/tools/demoserver.py
+++ b/runtime/tools/demoserver.py
@@ -11,37 +11,44 @@
#
# See ":help channel-demo" in Vim.
-import SocketServer
+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):
+class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
def handle(self):
- print "=== socket opened ==="
+ print("=== socket opened ===")
global thesocket
thesocket = self.request
while True:
try:
- data = self.request.recv(4096)
+ data = self.request.recv(4096).decode('utf-8')
except socket.error:
- print "=== socket error ==="
+ print("=== socket error ===")
break
except IOError:
- print "=== socket closed ==="
+ print("=== socket closed ===")
break
if data == '':
- print "=== socket closed ==="
+ print("=== socket closed ===")
break
- print "received: {}".format(data)
+ print("received: {}".format(data))
try:
decoded = json.loads(data)
except ValueError:
- print "json decoding failed"
+ print("json decoding failed")
decoded = [0, '']
if decoded[1] == 'hello!':
@@ -49,11 +56,11 @@ class ThreadedTCPRequestHandler(SocketSe
else:
response = "what?"
encoded = json.dumps([decoded[0], response])
- print "sending {}".format(encoded)
- self.request.sendall(encoded)
+ print("sending {}".format(encoded))
+ self.request.sendall(encoded.encode('utf-8'))
thesocket = None
-class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
+class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
pass
if __name__ == "__main__":
@@ -69,19 +76,19 @@ if __name__ == "__main__":
# 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("Server loop running in thread: ", server_thread.name)
- print "Listening on port {}".format(PORT)
+ print("Listening on port {}".format(PORT))
while True:
typed = sys.stdin.readline()
if "quit" in typed:
- print "Goodbye!"
+ print("Goodbye!")
break
if thesocket is None:
- print "No socket yet"
+ print("No socket yet")
else:
- print "sending {}".format(typed)
- thesocket.sendall(typed)
+ print("sending {}".format(typed))
+ thesocket.sendall(typed.encode('utf-8'))
server.shutdown()
server.server_close()