-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawl.py
91 lines (67 loc) · 2.85 KB
/
crawl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
""" 402 Crawler
Crawl endpoints, check socket connection, and
check 402 headers.
"""
import os
import json
import datetime
import logging
import socket
import requests
from two1.commands.config import Config
from two1.lib.wallet import Wallet
from two1.lib.bitrequests import BitTransferRequests
class Crawler402():
""" Crawl endpoints to check status.
Check server socket connection and query endpoints for
price and recipient address.
"""
def __init__(self, endpoint_list, log_file):
"""Set up logging & member vars"""
# configure logging
logging.basicConfig(level=logging.INFO,
filename=log_file,
filemode='a',
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M')
self.console = logging.StreamHandler()
self.console.setLevel(logging.INFO)
logging.getLogger('402-crawler').addHandler(self.console)
self.logger = logging.getLogger('402-crawler')
self.endpoint_list = endpoint_list
def check_endpoints(self):
"""Crawl 402 endpoints"""
# create 402 client
self.bitrequests = BitTransferRequests(Wallet(), Config().username)
# crawl endpoints, check headers
self.logger.info("\nCrawling machine-payable endpoints...")
for endpoint in self.endpoint_list:
# extract domain name
name = endpoint.split('/',1)[0].split('.',1)[1]
# get server ip
server_ip = socket.gethostbyname(name)
# self.logger.info("Checking {0} on port {1}".format(server_ip, port))
self.logger.info("Checking {}...".format(endpoint))
# configure socket module
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_state = sock.connect_ex((server_ip, 80))
sock.close()
if server_state == 0:
try:
self.logger.info("Server state: {} is up!".format(endpoint))
response = self.bitrequests.get_402_info('https://'+endpoint)
self.logger.info("Price: {}".format(response['price']))
self.logger.info("Address: {}".format(response['bitcoin-address']))
except Exception as e:
self.logger.info("Could not read 402 payment headers.")
else:
self.logger.info("Server state: {} is down!".format('https://'+endpoint))
self.logger.info("Timestamp: {}\n".format(datetime.datetime.now()))
if __name__=='__main__':
# 402 endpoints to crawl
endpoint_list = [
'market.21.co/search/bing',
'market.21.co/phone/send-sms',
]
crawler = Crawler402( endpoint_list, '402-crawler.log')
crawler.check_endpoints()