Skip to content

Commit

Permalink
Support multiple sockets and multiple hosts per connection
Browse files Browse the repository at this point in the history
  • Loading branch information
thelazt committed Aug 11, 2022
1 parent 9a778ba commit 098fd7e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
2 changes: 1 addition & 1 deletion client/wake.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ if [[ $# -eq 0 ]] ; then
exit 1
else
for HOST in $@ ; do
if result=$(echo -n ${HOST} | nc -w${WAKETIME} ${WAKEHOST} ${WAKEPORT} 2>/dev/null) ; then
if result=$(echo -n ${HOST} | nc -N -w${WAKETIME} ${WAKEHOST} ${WAKEPORT} 2>/dev/null) ; then
printf " - %-20s [%s]\n" ${HOST} "${result}"
else
echo "Connection to i4 Wake-On-LAN Server at ${WAKEHOST}:${WAKEPORT} failed - abort!"
Expand Down
47 changes: 27 additions & 20 deletions wol.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from pyparsing import *
from typing import Dict, Optional
import socketserver
import argparse
import logging
import psutil
Expand Down Expand Up @@ -88,29 +89,35 @@ def wake(hostname: str) -> bool:
logging.warning('Unknown host "{}"'.format(hostname))
return False

def listen(host: str, port: int) -> None:
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((host, port))
sock.listen(1)
logging.info('Listening on {}:{}'.format(host, port))
while True:
connection, client = sock.accept()
try:
hostname = connection.recv(256)
class WakeRequestHandler(socketserver.StreamRequestHandler):
def handle(self):
self.connection.settimeout(6)
client = self.client_address[0]
logging.debug('Connected {}'.format(client))
try:
while self.rfile:
hostname = self.rfile.readline().decode('ascii').strip()
if hostname:
logging.info('Request WoL at "{}" from {}'.format(hostname, client))
connection.sendall(b'success' if wake(hostname.decode('ascii').strip()) else b'failed')
except KeyboardInterrupt:
break
finally:
connection.close()
except Exception as e:
logging.exception('Listening on {}:{} failed'.format(host, port))
finally:
sock.close()
self.wfile.write(b"success\n" if wake(hostname) else b"failed\n")
else:
break
except socket.timeout:
logging.debug('Timeout of {}'.format(client))

def listen(host: str, port: int) -> None:
with socketserver.ForkingTCPServer((host, port), WakeRequestHandler, False) as server:
server.socket_type = socket.SOCK_STREAM
server.allow_reuse_address = True
server.server_bind()
server.server_activate()
logging.info('Listening on {}:{}'.format(host, port))
try:
server.serve_forever()
except KeyboardInterrupt:
logging.info('Exit due to keyboard interrupt')
server.server_close()
sys.exit(0)

if __name__ == '__main__':
parser = argparse.ArgumentParser(description='i4 WakeOnLan Util')
Expand Down

0 comments on commit 098fd7e

Please sign in to comment.