Py: Difference between revisions
Jump to navigation
Jump to search
(Python page for stuff #ff6600) |
(→Socket to Keys: TCP/IP Volume Control) |
||
| (One intermediate revision by one other user not shown) | |||
| Line 19: | Line 19: | ||
== Downloads == | == Downloads == | ||
https://www.python.org/downloads/ | https://www.python.org/downloads/ | ||
*Note on windows a system update may first be required to prevent installation errors. | |||
== Docs == | == Docs == | ||
https://www.python.org/doc/ | https://www.python.org/doc/ | ||
== Projects == | |||
=== Socket to Keys === | |||
Original code from Sergio http://sergiosprojects.blogspot.com/2012/01/python-server-keyboard-emulation.html | |||
Modified and tested code on Windows 7 with Python 3.5.0 | |||
* Volume Mute (toggle) | |||
**http://localhost:12345?cmd=M | |||
* Volume Up | |||
**http://localhost:12345?cmd=U | |||
* Volume Down | |||
**http://localhost:12345?cmd=D | |||
<pre> | |||
#!/usr/bin/env python | |||
import os | |||
import sys | |||
import socket | |||
import ctypes | |||
# Used to listen for connections | |||
LISTEN_ADDRESS = '' | |||
LISTEN_PORT = 12345 | |||
BUFFER_SIZE = 512 | |||
# Used for emulating key presses. | |||
user32 = ctypes.windll.user32 | |||
# Virtual keys | |||
VK_VOLUME_MUTE = 0xAD | |||
VK_VOLUME_DOWN = 0xAE | |||
VK_VOLUME_UP = 0xAF | |||
# Mapping expected input to actions | |||
bytes_to_keys = {} | |||
bytes_to_keys["M"] = VK_VOLUME_MUTE | |||
bytes_to_keys["D"] = VK_VOLUME_DOWN | |||
bytes_to_keys["U"] = VK_VOLUME_UP | |||
def run_command(cmd): | |||
os.system(cmd) | |||
def do_action(char): | |||
'''Takes a 1-character code and executes the action assoliated with it''' | |||
if char in bytes_to_keys: | |||
key_code = bytes_to_keys[char] | |||
user32.keybd_event(key_code, 0, 0, 0) # press | |||
user32.keybd_event(key_code, 0, 2, 0) # release | |||
else: | |||
print("unknown instruction:", char) | |||
########################################## | |||
################## Main ################## | |||
########################################## | |||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |||
s.bind((LISTEN_ADDRESS, LISTEN_PORT)) | |||
print("Starting server on port", LISTEN_PORT) | |||
s.listen(1) | |||
while True: | |||
conn, addr = s.accept() | |||
print('Connection address:', addr) | |||
try: | |||
while True: | |||
data = conn.recv(BUFFER_SIZE) | |||
if not data: break | |||
str_data = bytes.decode(data) # data is non-unicode, but all python strings need to be | |||
print("received data: \"{0}\"".format(str_data)) | |||
cmdIndex = str_data.find("?cmd=") + 5 | |||
cmdString = str_data[cmdIndex:cmdIndex+1] | |||
print("Command String:", cmdString); | |||
do_action(cmdString) | |||
break | |||
print("Client disconnected") | |||
except socket.error as e: | |||
print("Socket error:", e) | |||
except socket.timeout: | |||
print("Connection timed-out") | |||
except Exception as e: | |||
print("Exception:", e) | |||
finally: | |||
conn.close() | |||
print("Exit") | |||
</pre> | |||
Latest revision as of 20:26, 20 October 2015
,-.----.
\ / \ ___ ,---,
| : \ ,--.'|_ ,--.' |
| | .\ : | | :,' | | : ,---. ,---,
. : |: | : : ' : : : : ' ,'\ ,-+-. / |
| | \ : .--,.;__,' / : | |,--. / / | ,--.'|' |
| : . / /_ ./|| | | | : ' |. ; ,. :| | ,"' |
; | |`-', ' , ' ::__,'| : | | /' :' | |: :| | / | |
| | ; /___/ \: | ' : |__ ' : | | |' | .; :| | | | |
: ' | . \ ' | | | '.'|| | ' | :| : || | | |/
: : : \ ; : ; : ;| : :_:,' \ \ / | | |--'
| | : \ \ ; | , / | | ,' `----' | |/
`---'.| : \ \ ---`-' `--'' '---'
`---` \ ' ;
`--`
Downloads[edit | edit source]
https://www.python.org/downloads/
- Note on windows a system update may first be required to prevent installation errors.
Docs[edit | edit source]
Projects[edit | edit source]
Socket to Keys[edit | edit source]
Original code from Sergio http://sergiosprojects.blogspot.com/2012/01/python-server-keyboard-emulation.html
Modified and tested code on Windows 7 with Python 3.5.0
- Volume Mute (toggle)
- Volume Up
- Volume Down
#!/usr/bin/env python
import os
import sys
import socket
import ctypes
# Used to listen for connections
LISTEN_ADDRESS = ''
LISTEN_PORT = 12345
BUFFER_SIZE = 512
# Used for emulating key presses.
user32 = ctypes.windll.user32
# Virtual keys
VK_VOLUME_MUTE = 0xAD
VK_VOLUME_DOWN = 0xAE
VK_VOLUME_UP = 0xAF
# Mapping expected input to actions
bytes_to_keys = {}
bytes_to_keys["M"] = VK_VOLUME_MUTE
bytes_to_keys["D"] = VK_VOLUME_DOWN
bytes_to_keys["U"] = VK_VOLUME_UP
def run_command(cmd):
os.system(cmd)
def do_action(char):
'''Takes a 1-character code and executes the action assoliated with it'''
if char in bytes_to_keys:
key_code = bytes_to_keys[char]
user32.keybd_event(key_code, 0, 0, 0) # press
user32.keybd_event(key_code, 0, 2, 0) # release
else:
print("unknown instruction:", char)
##########################################
################## Main ##################
##########################################
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((LISTEN_ADDRESS, LISTEN_PORT))
print("Starting server on port", LISTEN_PORT)
s.listen(1)
while True:
conn, addr = s.accept()
print('Connection address:', addr)
try:
while True:
data = conn.recv(BUFFER_SIZE)
if not data: break
str_data = bytes.decode(data) # data is non-unicode, but all python strings need to be
print("received data: \"{0}\"".format(str_data))
cmdIndex = str_data.find("?cmd=") + 5
cmdString = str_data[cmdIndex:cmdIndex+1]
print("Command String:", cmdString);
do_action(cmdString)
break
print("Client disconnected")
except socket.error as e:
print("Socket error:", e)
except socket.timeout:
print("Connection timed-out")
except Exception as e:
print("Exception:", e)
finally:
conn.close()
print("Exit")