keyboard-hook/usr/bin/server.py

44 lines
1.3 KiB
Python
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#coding:utf-8
import os
from socket import *
NULL_CHAR = chr(0)
def write_report(report):
with open('/dev/hidg0','rb+') as fd:
fd.write(report)
#''代表服务器为 localhost
myHost = ''
#在一个非保留端口号上进行监听
myPort = 50009
#设置一个TCP socket对象
sockobj = socket(AF_INET, SOCK_STREAM)
#绑定端口号
sockobj.bind((myHost, myPort))
#监听允许5个连结
sockobj.listen(5)
#直到进程结束时才结束循环
while True:
#等待客户端连接
connection, address = sockobj.accept( )
#连接是一个新的socket
print ('Server connected by', address)
while True:
#读取客户端套接字的下一行
data = connection.recv(1024)
#如果没有数量的话,那么跳出循环
if not data: break
if data == "hid open":
os.popen('ls /sys/class/udc | xargs echo > /sys/kernel/config/usb_gadget/g1/UDC')
print("hid open")
elif data == "hid close":
os.popen('echo > /sys/kernel/config/usb_gadget/g1/UDC')
print("hid close")
else:
write_report(data)
#发送一个回复至客户端
connection.send('server => ' + data)
#当socket关闭时eof
connection.close( )