Created
August 3, 2014 15:18
-
-
Save farhaven/785d72cc573a39e338ef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
import socket | |
import struct | |
def tryVNC(host, port=5900): | |
try: | |
s = socket.create_connection((host, port)) | |
except socket.error: | |
return None | |
# version negotiation | |
try: | |
srv_version = s.recv(3 + 1 + 3 + 1 + 3 + 1) | |
except socket.timeout: | |
return None | |
if len(srv_version) < 3 + 1 + 3 + 1 + 3 + 1: | |
return None | |
assert s.send(srv_version) == len(srv_version) | |
# security negotiation | |
try: | |
security = bytes(s.recv(2)) | |
except socket.timeout: | |
return None | |
if len(security) != 2 or security[1] != "\x01": | |
print(repr(security), len(security)) | |
return None | |
# attempt authentication | |
assert s.send(security[1]) == 1 | |
try: | |
security_result = bytes(s.recv(4)) | |
except socket.timeout: | |
return None | |
assert security_result == bytes('\x00' * 4) | |
# send "share desktop" request | |
assert s.send('\x01') == 1 | |
# get framebuffer parameters | |
try: | |
rv = dict() | |
rv["width"] = struct.unpack("!H", s.recv(2))[0] | |
rv["height"] = struct.unpack("!H", s.recv(2))[0] | |
s.recv(1 + 1) | |
s.recv(1 + 1) | |
s.recv(2 + 2 + 2) | |
s.recv(1 + 1 + 1) | |
s.recv(3) | |
namelen = struct.unpack("!L", s.recv(4))[0] | |
rv["name"] = s.recv(namelen) | |
except socket.timeout: | |
return None | |
return rv |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment