Last active
September 3, 2015 14:05
-
-
Save fabianosarracco/b83f00dd120b0420c54e to your computer and use it in GitHub Desktop.
This file contains 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
# -*- coding: utf-8 -*- | |
""" | |
jarpis_finder.py | |
~~~~~~~~~ | |
This module allows to find and return the IP address of Jarpis | |
if it is found in the current LAN. | |
""" | |
import grequests | |
from grequests import send | |
import gevent | |
from gevent.pool import Pool | |
from urlparse import urlparse | |
import sys | |
def exception_handler(request, exception): | |
""" | |
Ignore exceptions. | |
""" | |
pass | |
def checkIPs(ips): | |
size = 32 # Number of concurrent requests | |
pool = Pool(size) if size else None | |
for iteration in range(0, 256 / size): | |
urls = ["http://%s.%s:42001/api/status" % (ips, i) for i in range(iteration*size, (iteration+1)*size)] | |
rs = (grequests.get(u, timeout=3) for u in urls) | |
requests = list(rs) | |
jobs = [send(r, pool, stream=None) for r in requests] | |
gevent.joinall(jobs) | |
for request in requests: | |
if request.response: | |
return urlparse(request.url).hostname | |
elif exception_handler: | |
exception_handler(request, request.exception) | |
if __name__ == "__main__": | |
res = checkIPs("192.168.0") | |
if res is not None: | |
print res | |
sys.exit(0) | |
res = checkIPs("10.10.10") | |
if res is not None: | |
print res | |
sys.exit(0) | |
res = checkIPs("192.168.1") | |
if res is not None: | |
print res | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment