def setup_socket(ip, timeout): ''' Open a socket using an instance of http.client.HTTPConnection. :param ip: IP address :param timeout: Timeout for socket operations :returns: A tuple of: Error message or None, an instance of http.client.HTTPConnection. ''' logger = logging.getLogger('default') client = http.client.HTTPConnection(ip, timeout=timeout) client.auto_open = 0 try: client.connect() except socket.timeout: logger.error('Connecting to {} timed out.'.format(ip)) return ('socket.timeout', None) except OSError as e: if e.errno is None: logger.error('Connecting to {} failed: {}'.format(ip, e)) return (str(e), None) else: logger.error('Connecting to {} failed: {}'.format(ip, e.strerror)) return (e.strerror, None) else: return (None, client)
def play(room_id, token, server, debug=False, alias=''): """ Connect to game server and play rounds in the loop until end of game. """ # connect to the game server client = http.client.HTTPConnection(server) client.connect() game_url = '/games/{}/board?token={}'.format(room_id, token) if alias: game_url += '&alias={}'.format(alias) # wait until the game starts client.request('GET', game_url) response = client.getresponse() while response.status == 200: data = json.loads(response.read().decode()) if debug: print(data) # sleep 3 seconds so, you will be able to read printed data time.sleep(3) # make your move and wait for a new round client.request('POST', game_url, json.dumps(get_move(data))) response = client.getresponse()
def single_page(course, quarter): while (1): try: client = http.client.HTTPSConnection(host) client.connect() ''' client.request("GET",url) res = client.getresponse() ''' ############################ body[quarter_key] = quarter body[course_key] = course #print(body) data = urllib.parse.urlencode(body) client.request("POST", url, data, headers) res = client.getresponse() #print(res.getheaders()) raw_codes = res.read() html = gzip.decompress(raw_codes) html = html.decode("utf-8") ################################## filepath = "./raw_data/" + course + "_" + quarter + ".html" f = open(filepath, "w+") f.write(html) f.close() client.close() time.sleep(0.1) break ############################### except: print(raw_codes) print("error when reading this page!!!skipping!") break
def recurse(subreddit, hot_list=[], after=None, client=None): """Get all hot posts on a Subreddit""" path = '/r/' + urllib.parse.quote(subreddit, safe='') + '/hot.json' path += '?raw_json=1' if after is not None: path += '&after=' + urllib.parse.quote_plus(after) path += '&count=' + str(len(hot_list)) if client is None: client = http.client.HTTPSConnection('www.reddit.com') client.connect() client.putrequest('GET', path) client.putheader('Connection', 'keep-alive') client.putheader('User-Agent', 'python:hbtn701t2:1 (by /u/SamHermesBoots)') client.endheaders() response = client.getresponse() if response.status != 200: client.close() return None posts = json.load(io.TextIOWrapper(response, encoding='UTF-8')) if response.getheader('Connection', 'close') == 'close': client.close() client = None hot_list.extend(p['data']['title'] for p in posts['data']['children']) if posts['data']['after'] is None: client.close() return hot_list return recurse(subreddit, hot_list, posts['data']['after'], client)
def play(room_id, token, server, debug=False, alias=''): """ Connect to game server and play rounds in the loop until end of game. """ # connect to the game server client = http.client.HTTPConnection(server) client.connect() game_url = '/games/{}/board?token={}'.format(room_id, token) if alias: game_url += '&alias={}'.format(alias) # wait until the game starts client.request('GET', game_url) response = client.getresponse() while response.status == 200: data = json.loads(response.read().decode()) #debug = True if debug: #print(data) pp=pprint.PrettyPrinter(indent=4) pp.pprint(data) # sleep 3 seconds so, you will be able to read printed data time.sleep(3) # make your move and wait for a new round client.request('POST', game_url, json.dumps(get_move(data))) response = client.getresponse()
def do_server_play(token, game, strategy): client = http.client.HTTPConnection(SERVER, 8080) client.connect() # block until the game starts client.request('GET', '/games/{}/board?token={}'.format(game, token)) i = 0 response = client.getresponse() while response.status == 200: data = json.loads(response.read().decode()) print("[***] step {}, current score {}, moves we have left {}".format( i, data["score"], data["moves"])) next_cursor = solve( data["board"], strategy, data["score"] ) time.sleep(0.2) # make move and wait for a new round client.request( 'POST', '/games/{}/board?token={}'.format(game, token), json.dumps({ 'x': next_cursor.x, 'y': next_cursor.y, }) ) response = client.getresponse() i += 1 else: print("[i] response done, got server code {}".format(response.status))
def main_program(): """This program subscribes to the mqtt server and publishes specfic data items to thingspeak channels """ time_cntr = time.time() # set up logging FORMAT = '%(asctime)-15s %(message)s' logging.basicConfig(filename='thingspeak.log', level=config["general"].getint("loglevel"), format=FORMAT) logging.info('Thingspeak starting') # Connect to the mqtt server client = mqtt.Client() client.on_connect = on_connect client.on_message = on_message client.connect(config['mqtt'].get('ip_address','127.0.0.1'), config['mqtt'].getint('port', 1883), config['mqtt'].getint('timeout',60)) # Blocking call that processes network traffic, dispatches callbacks and # handles reconnecting. # Other loop*() functions are available that give a threaded interface and a # manual interface. client.loop_start() while True: time.sleep(1) if time_cntr + 60 < (time.time()): time_cntr = time.time() config.read(config_fname)
def get_expected(): try: with socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) as client: client.connect(('2400:3200::1', 53)) return client.getsockname()[0] except Exception as e: logging.error(e)
def clientit2(): """ :return: """ client = socket() client.connect(('127.0.0.1', 4321)) in_datas = bytes() # receive batch data = client.recv(4096) while data: in_datas += data data = client.recv(4096) my_dict = json.loads(in_datas.decode()) filename = 'new_{}'.format(my_dict['filename']) filedata = my_dict['filedata'].encode() with open(filename, 'wb') as fw: fw.write(base64.b64decode(filedata)) client.close()
def start_mqtt_listener(event_queue, mqtt_server_host, mqtt_port): def on_mqtt_connect(client, _userdata, _flags, _rc): client.subscribe(SL5G_IP_TOPIC) def on_mqtt_publish(_client, _userdata, msg): panel_id = re.match(SL5G_PANEL_REGEX, msg.topic).group(1) event_queue.post_found_ip(panel_id, msg.payload.decode("utf-8")) client = mqtt.Client() client.on_connect = on_mqtt_connect client.on_message = on_mqtt_publish try: client.connect(mqtt_server_host, mqtt_port, 60) except OSError as e: print("Could not connect to MQTT server: {}".format(e)) event_queue.post_shutdown() def shutdown(): client.loop_stop() event_queue.post_register_shutdown_hook(shutdown) client.loop_start() return client
def extract_html_page(target_host): global my_var target_host2 = urlparse(target_host).netloc if not os.path.exists(dstPath): os.makedirs(dstPath) if not (os.path.isdir(os.path.join(dstPath, target_host2))): os.mkdir(os.path.join(dstPath, target_host2)) client = "RIWEB_CRAWLER" target_port = 80 client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect((target_host2, target_port)) request = "GET {} HTTP/1.1\r\nHost: {}\r\nUser-Agent: {}\r\n\r\n".format( target_host, target_host2, client) client.send(request.encode()) #din lab6 CHUNK_SIZE = 16 buffer = bytearray() buffer.extend(client.recv(CHUNK_SIZE)) firstline = buffer.decode() data = b'' # verificarea codului de stare (daca acesta este cod de eroare, se va deschide un fisier text în care se va # scrie cererea initiala si raspunsul serverului); if '200 OK' in firstline: buffer_size = 4096 data1 = b'' while True: part = client.recv(buffer_size) data1 += part if b'</html>' in part.lower() or len(part) == 0: break data = data1 data = data.decode() data_lower = data.lower() if data_lower.find('<!doctype') > -1: data_to_store = data[data_lower.find('<!doctype' ):data_lower.find('</html>') + 8] else: data_to_store = data[data_lower. find('<html'):data_lower.find('</html>') + 8] # salvarea continutului raspunsului (corpul mesajului – pagina html) într-un fisier html with open('my_page.html', 'w') as file: file.write(data_to_store) else: data = data + buffer buffer_size = 4096 data1 = b'' while True: part = client.recv(buffer_size) data1 += part if b'</html>' in part.lower() or len(part) == 0: break data += data1 with open('error_page.txt', 'w') as file: file.write(data.decode()) pass
def mqtt_connect(): broker_address = "localhost" print("creating new instance") client = mqtt.Client("P1") #create new instance client.connect(broker_address) #connect to broker print("connecting to broker") client.subscribe("mesh_gateway/data") print("Subscribing to topic", "mesh_gateway/data") return client
def sendMessage(m, sessionid, type="query"): client = http.client.HTTPConnection(janet_host, janet_port) client.connect() headers = {'Content-type': 'application/json'} query = '&content=' + m + '&user_id=' + sessionid + '&type=' + type + '&' #Si os preguntais por que & al principio y al final pues yo que se, pero asi funciona json_data = json.dumps(query) client.request("POST", "/api", json_data, headers) response = client.getresponse() responseString = response.read().decode('utf-8') client.close() return responseString
def connect(self, job, pcs, config): client = http.client.HTTPConnection(str(job.ip), timeout=self.conn_timeout) client.auto_open = 0 try: client.connect() except socket.timeout: return Connection(None, None, CONN_TIMEOUT) except OSError as e: return Connection(None, None, CONN_FAILED) else: return Connection(client, client.sock.getsockname()[1], CONN_OK)
def init_mqtt(url="localhost", port=1883, keep_alive=60): """Init MQTT connection""" client = mqtt.Client() client.on_connect = on_connect client.on_message = on_message try: print(url, port, keep_alive) client.connect(url, port, keep_alive) client.loop_start() return client except Exception as e: log.exception(e) return None
def extract_html_page(target_host): global my_var #target_host2 = "riweb.tibeica.com" target_host2 = urlparse(target_host).netloc if not (os.path.isdir(os.path.join('work_directory', target_host2))): os.mkdir(os.path.join('work_directory', target_host2)) client = "RIWEB_CRAWLER" target_port = 80 # create a socket object client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # connect the client client.connect((target_host2, target_port)) # send some data request = "GET {} HTTP/1.1\r\nHost: {}\r\nUser-Agent: {}\r\n\r\n".format( target_host, target_host2, client) client.send(request.encode()) # =========================Read step by step=============================== CHUNK_SIZE = 36 # you can set it larger or smaller lines = [] buffer = bytearray() buffer.extend(client.recv(CHUNK_SIZE)) firstline = buffer[:buffer.find(b'\n')] firstline = buffer.decode() data = b'' #print(target_host) #print(buffer) if '200 OK' in firstline: data = recvall(client) data = data.decode() data_lower = data.lower() # print(data_lower.find('<!doctype')) if data_lower.find('<!doctype') > -1: data_to_store = data[data_lower.find('<!doctype' ):data_lower.find('</html>') + 8] else: data_to_store = data[data_lower. find('<html'):data_lower.find('</html>') + 8] #client.close() return data_to_store else: print(firstline) print(target_host) data = data + buffer data += recvall(client) with open('error_page.txt', 'w') as file: file.write(data.decode()) pass
def scan(duration=None): MQTT_BROKER_HOSTNAME = "broker.mqttdashboard.com" print("Client") client = mqtt.Client() try: client.connect(MQTT_BROKER_HOSTNAME, 1883, 60) except Exception as e: print(e) print("ClientFound") """ Scan for beacons. This function scans for [duration] seconds. If duration is set to None, it scans until interrupted. """ print("Scanning...") subprocess.call("sudo hciconfig hci0 reset", shell=True, stdout=DEVNULL) lescan = subprocess.Popen( ["sudo", "-n", "hcitool", "lescan", "--duplicates"], stdout=DEVNULL) dump = subprocess.Popen(["sudo", "-n", "hcidump", "--raw"], stdout=subprocess.PIPE) packet = None try: startTime = time.time() for line in dump.stdout: line = line.decode() if line.startswith("> "): if packet: onPacketFound(packet) packet = line[2:].strip() elif line.startswith("< "): if packet: onPacketFound(packet) packet = None else: if packet: packet += " " + line.strip() if duration and time.time() - startTime > duration: break except KeyboardInterrupt: pass subprocess.call(["sudo", "kill", str(dump.pid), "-s", "SIGINT"]) subprocess.call(["sudo", "-n", "kill", str(lescan.pid), "-s", "SIGINT"])
def main(): options, server, remote = parse_options() password = "" if options.readpass: password = getpass.getpass("Enter SSH password: "******"Connecting to ssh host %s:%d ..." % (server[0], server[1])) try: proxy_uri = "" url = urllib.parse.urlparse(proxy_uri) http_con = http.client.HTTPConnection(url.hostname, url.port) headers = {} if url.username and url.password: auth = '%s:%s' % (url.username, url.password) headers['Proxy-Authorization'] = 'Basic ' + base64.b64encode(auth) http_con.set_tunnel(server[0], server[1], headers) http_con.connect() sock = http_con.sock #sock = http_proxy_tunnel_connect(proxy=("",),target=(server[0],server[1]),timeout=500) data = sock.recv(5) print(data.decode()) sock.send("HELLO BACK".encode()) #sock.send("\nClient: HELLO BACK".encode()) #print(data.decode()) client.connect(hostname=server[0], port=server[1], sock=sock, username=options.user, password=password, banner_timeout=4) except Exception as e: print( ("*** Failed to connect to %s:%d: %r" % (server[0], server[1], e))) raise e sys.exit(1) verbose("Now forwarding remote port %d to %s:%d ..." % (options.port, remote[0], remote[1])) try: reverse_forward_tunnel(options.port, remote[0], remote[1], client.get_transport()) except KeyboardInterrupt: print("C-c: Port forwarding stopped.") sys.exit(0)
def clientit(): """ received: 2019-05-28 14:37:07.234834 :return: """ client = socket() client.connect(('127.0.0.1', 4321)) data = client.recv(4096).decode('utf-8') print('received: {}'.format(data)) client.close()
def probe_topic(self, topics=None, on_message=default_on_message): if topics is None: topics = [] def __on_broker_connected(client, userdata, flags, rc): print("Connected with result code " + str(rc)) for topic in topics: client.subscribe(topic) client = mqtt.Client(client_id="oc-client") client.on_connect = __on_broker_connected client.on_message = on_message client.username_pw_set(self.config.broker_username, password=self.config.broker_password) client.connect(self.config.broker_host, int(self.config.broker_port), 60) return client
def top_ten(subreddit): """Get the 10 top posts on a Subreddit""" path = '/r/' + urllib.parse.quote(subreddit, safe='') + '/hot.json' path += '?raw_json=1&limit=10' client = http.client.HTTPSConnection('www.reddit.com') client.connect() client.putrequest('GET', path) client.putheader('Connection', 'close') client.putheader('User-Agent', 'python:hbtn701t1:1 (by /u/SamHermesBoots)') client.endheaders() response = client.getresponse() if response.status != 200: client.close() print(None) return posts = json.load(io.TextIOWrapper(response, encoding='UTF-8')) client.close() for post in posts['data']['children']: print(post['data']['title'])
def connect(self, password=None): """ Connect, login, and retrieve the track list. """ try: client = DAAPClient() if AUTH and password: client.connect(self.server, self.port, password, self.user_agent) else: client.connect(self.server, self.port, None, self.user_agent) self.session = client.login() self.connected = True # except DAAPError: except Exception: logger.exception('failed to connect to ({0},{1})'.format( self.server, self.port)) self.auth = True self.connected = False raise
def test_connection(self): """ Testing connection via gotten connector """ if self._connector.__name__ == "_tcp_connector": try: client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ip, port = self._connection_string.split(":") client.connect((ip, int(port))) client.close() return True except Exception as err: print(f"Error: Cannot connect to Rspamd: {err} : {RSPAMD_HTTP_SOCKET}") return False else: try: client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) client.connect(self._connection_string) client.close() return True except Exception as err: print(f"Error: Cannot connect to Rspamd: {err} : {RSPAMD_HTTP_SOCKET}") return False
def _unix_connector(self, message): """ : param message: bytes """ CRLF = "\r\n" init_line = ["POST /checkv2 HTTP/1.1"] self.add_header("Content-Length", len(message)) headers = init_line + [f"{header[0]}: {header[1]}" for header in self._headers] headers = (CRLF.join(headers) + 2*CRLF).encode("utf8") client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) client.connect(self._connection_string) raddr = client.getpeername() print(f"{self.msg_id}: localhost -> {raddr}: Connected to maild.", on_debug=True) print(f"{self.msg_id}: localhost -> {raddr}: Send message to maild.", on_debug=True) client.send(headers + message) print(f"{self.msg_id}: localhost <- {raddr}: Waiting for response from maild.", on_debug=True) rspamd_result = client.recv(1024) if not rspamd_result: return {"error": "Error: Rspamd server is not responding"} headers, body = rspamd_result.decode("utf8").split("\r\n\r\n") client.close() return json.loads(body)
def connect(self): logger.debug( "Opening SSH connection to {host}:{port}".format( host=self.host, port=self.port)) client = SSHClient() client.load_system_host_keys() client.set_missing_host_key_policy(AutoAddPolicy()) try: client.connect( self.host, port=self.port, username=self.username, timeout=self.timeout, ) except ValueError as e: logger.error(e) logger.warning( """ Patching Crypto.Cipher.AES.new and making another attempt. See here for the details: http://uucode.com/blog/2015/02/20/workaround-for-ctr-mode-needs-counter-parameter-not-iv/ """) client.close() import Crypto.Cipher.AES orig_new = Crypto.Cipher.AES.new def fixed_AES_new(key, *ls): if Crypto.Cipher.AES.MODE_CTR == ls[0]: ls = list(ls) ls[1] = '' return orig_new(key, *ls) Crypto.Cipher.AES.new = fixed_AES_new client.connect( self.host, port=self.port, username=self.username, timeout=self.timeout, ) return client
def http_connect_tunnel(master: str, service: str, cert_file: Optional[str], cert_name: Optional[str]) -> None: parsed_master = request.parse_master_address(master) assert parsed_master.hostname is not None, "Failed to parse master address: {}".format( master) if parsed_master.scheme == "https": context = ssl.create_default_context(cafile=cert_file) client = HTTPSProxyConnection( cert_name, parsed_master.hostname, parsed_master.port, context=context) # type: http.client.HTTPConnection else: client = http.client.HTTPConnection(parsed_master.hostname, parsed_master.port) client.set_tunnel(service) try: client.connect() except socket.gaierror: print("failed to look up host:", master, file=sys.stderr) raise with client.sock as sock: sock = SocketWrapper(sock) # Directly using sys.stdin.buffer.read or sys.stdout.buffer.write would block due to # buffering; instead, we use unbuffered file objects based on the same file descriptors. unbuffered_stdin = os.fdopen(sys.stdin.fileno(), "rb", buffering=0) unbuffered_stdout = os.fdopen(sys.stdout.fileno(), "wb", buffering=0) c1 = Copier(sock, unbuffered_stdout) c2 = Copier(unbuffered_stdin, sock) c1.start() c2.start() c1.join() c2.join()
def ConnectLoop(): data = json.load(open('config.json')) config = data["config"] mqttfeeds = config["feeds"] print(mqttfeeds) client_name = config["clientName"] hostname = config["host"] user = config["user"] pwd = config["pwd"] hostport = config["port"] print(client_name, hostname, user, pwd, hostport) client = mqtt.Client(client_name, userdata=mqttfeeds) print(client) client.username_pw_set(user, pwd) client.on_message = on_message client.on_connect = on_connect client.on_publish = on_publish client.on_subscribe = on_subscribe client.on_disconnect = on_disconnect # Uncomment to enable debug messages # client.on_log = on_log # B64 encoded client.tls_set("adafruit_ca.crt") client.connect(hostname, port=hostport) client.loop_forever() print("out of loop")
def single_page(course,quarter): while(1): try: client = http.client.HTTPSConnection(host) client.connect() ''' client.request("GET",url) res = client.getresponse() ''' ############################ body[quarter_key] = quarter body[course_key] = course #print(body) data = urllib.parse.urlencode(body) client.request("POST",url,data,headers) res = client.getresponse() #print(res.getheaders()) raw_codes = res.read() html = gzip.decompress(raw_codes) html = html.decode("utf-8") ################################## filepath = "./raw_data/"+course+"_"+quarter+".html" f = open(filepath,"w+") f.write(html) f.close() client.close() time.sleep(0.1) break ############################### except: print(raw_codes) print("error when reading this page!!!skipping!") break
def checkCertificate(url_base, url_rest, req_headers={}, request_type='GET'): connection = http.client.HTTPSConnection(url_base, timeout=1) certificate_is_valid = None certificate_host_names = None try: client = socket.socket() client.connect((url_base, 443)) client_ssl = Connection(Context(TLSv1_2_METHOD), client) client_ssl.set_connect_state() client_ssl.set_tlsext_host_name(url_base.encode('UTF-8')) client_ssl.do_handshake() certificate_host_names = get_certificate_hosts( client_ssl.get_peer_certificate()) except: pass finally: if client: client.close() try: connection.request(request_type, url_rest, headers=req_headers) certificate_is_valid = True except: certificate_is_valid = False return (certificate_is_valid, certificate_host_names)
def do_request(method='GET', body=None, headers={}, path='/'): url = urlparse(getenv('API_URL')) abs_path = '{}/api{}'.format(url.path, path) debug('{} request to {}:{}{}'.format(method, url.hostname, url.port, abs_path)) connection = connect(url.hostname, url.port) connection.request(method, abs_path, body=body, headers=headers) response = connection.getresponse() if response.status >= 200 and response.status < 300: return json_decode(response.read()) raise Exception('Unexpected response: {}, {}'.format( response.status, response.read()))
def test_chunked_encoding(monkeypatch, dev_server, send_length): stream, length, boundary = stream_encode_multipart({ "value": "this is text", "file": FileStorage( BytesIO(b"this is a file"), filename="test.txt", content_type="text/plain", ), }) headers = {"content-type": f"multipart/form-data; boundary={boundary}"} if send_length: headers["transfer-encoding"] = "chunked" headers["content-length"] = str(length) client = dev_server("data") # Small block size to produce multiple chunks. conn = client.connect(blocksize=128) conn.putrequest("POST", "/") conn.putheader("Transfer-Encoding", "chunked") conn.putheader("Content-Type", f"multipart/form-data; boundary={boundary}") # Sending the content-length header with chunked is invalid, but if # a client does send it the server should ignore it. Previously the # multipart parser would crash. Python's higher-level functions # won't send the header, which is why we use conn.put in this test. if send_length: conn.putheader("Content-Length", "invalid") conn.endheaders(stream, encode_chunked=True) r = conn.getresponse() data = json.load(r) r.close() assert data["form"]["value"] == "this is text" assert data["files"]["file"] == "this is a file" environ = data["environ"] assert environ["HTTP_TRANSFER_ENCODING"] == "chunked" assert "HTTP_CONTENT_LENGTH" not in environ assert environ["wsgi.input_terminated"]
import time import logging log = logging.getLogger('grot-client') SERVER = 'localhost' if __name__ == '__main__': token = sys.argv[1] # your Access Token game = sys.argv[2] # 0 (development mode), 1 (duel), 2 (contest) time.sleep(random.random()) # connect to the game server client = http.client.HTTPConnection(SERVER, 8080) client.connect() # block until the game starts client.request('GET', '/games/{}/board?token={}'.format(game, token)) response = client.getresponse() ''' { "score": 0, # obtained points "moves": 5, # available moves "moved": [None, None], # your last choice [x, y] "board": [ [ { "points": 1, "direction": "up",
'one form; see under -v for signification of NAME'); parser_post.add_argument('--xform', '-x', required=True, help='Xform to post') args = parser.parse_args() lo.setLevel(not args.debug and INFO or DEBUG) url = urllib.parse.urlparse(args.server) port = url.port or (url.scheme == 'https' and 443 or 80) hostname = url.netloc if ':' in url.netloc: hostname = url.netloc[:url.netloc.index(':')] port = int(url.netloc[url.netloc.index(':')+1:]) client = AggregateClient(hostname, port, url.path, scheme=url.scheme) client.connect(args.username, args.password) if args.command == 'post': with io.open(args.xform) as fd: form_xml = fd.read() form = XForm(form_xml) if args.csv: # post multiple forms with io.open(args.csv) as csvfd: reader = csv.reader(csvfd)
# Subscribing in on_connect() means that if we lose the connection and # reconnect then subscriptions will be renewed. client.subscribe("/door/front/mode") client.subscribe("/door/front/add") client.subscribe("/door/front/status") db = Database() client = mqtt.Client() client.on_connect = on_connect client.on_message = on_message ## Clears any retained messages on the mode channel, eliminates triggering a door open on boot and removes any retained message from clients client.publish("/door/front/mode", "Clear", retain=True) client.connect("192.168.68.112", 1883, 60) client.loop_start() GPIO.setmode(GPIO.BOARD) # Use physical pin numbering GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_UP) Alert("Door Lock Started") while (end_Thread == 0): input_state = GPIO.input(7) if (input_state == False): mode = db.fetch("mode") if (mode == 0):
def main_program(): """This program subscribes to the mqtt server and publishes specfic data items to thingspeak channels """ time_cntr = time.time() # set up logging FORMAT = '%(asctime)-15s %(message)s' logging.basicConfig(filename='thingspeak1a.log', level=config["general"].getint("loglevel"), format=FORMAT) logging.info('Thingspeak starting') # Connect to the mqtt server client = mqtt.Client() client.on_connect = on_connect client.on_message = on_message client.connect(config['mqtt'].get('ip_address','127.0.0.1'), config['mqtt'].getint('port', 1883), config['mqtt'].getint('timeout',60)) # Blocking call that processes network traffic, dispatches callbacks and # handles reconnecting. # Other loop*() functions are available that give a threaded interface and a # manual interface. client.loop_start() while True: time.sleep(1) if time_cntr + 60 < (time.time()): time_cntr = time.time() config.read(config_fname) print("Re-read config file") nCnt=0 for uD in updateList: # if it's taken longer than 5 seconds, send the update anyway timeDiff = datetime.datetime.now() - uD.init_dt if timeDiff.seconds > 5: logging.info("Timeout - sending update anyway") uD.complete = 1 if uD.complete == 1: headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain", "THINGSPEAKAPIKEY": uD.api_key} params = "field1={}".format(uD.field1) if uD.field2 != '': params = params + "&field2={}".format(uD.field2) if uD.field3 != '': params = params + "&field3={}".format(uD.field3) if uD.field4 != '': params = params + "&field4={}".format(uD.field4) if uD.field5 != '': params = params + "&field5={}".format(uD.field5) if uD.field6 != '': params = params + "&field6={}".format(uD.field6) if uD.field7 != '': params = params + "&field7={}".format(uD.field7) if uD.field8 != '': params = params + "&field8={}".format(uD.field8) print(params) try: conn = http.client.HTTPConnection(config['thingspeak'].get('url')) conn.request("POST", "/update", params, headers) response = conn.getresponse() logging.info("HTTP send success") conn.close() updateList.pop(nCnt) except: logging.warning("HTTP send error {}:{}".format(headers, params)) else: nCnt += 1
'<s:Envelope ' \ 'xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"' \ 's:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">' \ '<s:Body>' \ '<u:GetInsightParams xmlns:u="urn:Belkin:service:insight:1"></u:GetInsightParams>' \ '</s:Body>' \ '</s:Envelope>' conn = http.client.HTTPConnection(ipAddress, 49154) conn.request("POST", "/upnp/control/insight1", data2, headers) response = conn.getresponse() resp_data = response.read() if response.status == 200: conn.close() power_data_collections = ElementTree.fromstring( resp_data.decode()).find('.//InsightParams').text.split("|") client = mqtt.Client() client.username_pw_set('xpimatpq', 'r512yUWDQJO9') client.connect('10.112.10.127', 50844, 2) payload = { 'time': datetime.now().timestamp(), 'current_power': power_data_collections[7], } client.publish('power_usage', json.dumps(payload)) elif response.status == 403: print("ERROR: 403 (FORBIDDEN)") else: print("ERROR: " + str(response.status))
import paho.mqtt.client as mqtt import datetime import time import os datecount = 0 dateincreament = 7 mqttaddress = os.getenv('mqttaddress', '192.168.1.11') print("mqttaddress env value", mqttaddress) mqttport = os.getenv('mqttport', '1883') print("mqttport env value", mqttport) discord = os.getenv('discord', 'aaa') print("discord env value", discord) client = mqtt.Client() client.connect(mqttaddress, int(mqttport)) client.publish("topic/vaccine_status", "no", retain=True) webhook = Webhook.from_url(discord, adapter=RequestsWebhookAdapter()) while True: if datecount == 0: x = datetime.datetime.now() date1 = x.strftime("%d-%m-%Y") datecount = datecount + 1 elif datecount == 3: dateincreament = 7 datecount = 0 print("\n sleeping for 5min \n") time.sleep(300) client.disconnect() client.connect(mqttaddress, int(mqttport))