def request_raw(method, url, data=None, timeout=None): try: proto, dummy, host, path = url.split("/", 3) except ValueError: proto, dummy, host = url.split("/", 2) path = "" if proto != "http:": raise ValueError("Unsupported protocol: " + proto) try: host, port = host.split(":", 2) port = int(port) except ValueError: port = 80 if timeout: reader, writer = yield from asyncio.wait_for( asyncio.open_connection(host, port), timeout) else: reader, writer = yield from asyncio.run( asyncio.open_connection(host, port)) # Use protocol 1.0, because 1.1 always allows to use chunked transfer-encoding # But explicitly set Connection: close, even though this should be default for 1.0, # because some servers misbehave w/o it. content_length = '' if data is not None: content_length = "content-length: %i\r\n" % (len(data)) query = "%s /%s HTTP/1.0\r\nHost: %s:%i\r\n%sConnection: close\r\nUser-Agent: compat\r\n\r\n" % ( method, path, host, port, content_length) yield from writer.awrite(query.encode('latin-1')) if data: yield from writer.awrite(data) return reader
def request_raw(method, url): try: proto, dummy, host, path = url.split("/", 3) except ValueError: proto, dummy, host = url.split("/", 2) path = "" if proto != "http:": raise ValueError("Unsupported protocol: " + proto) port = 80 if ":" in host: host, port = host.split(":", 1) port = int(port) reader, writer = yield from asyncio.open_connection(host, port) # Use protocol 1.0, because 1.1 always allows to use chunked transfer-encoding # But explicitly set Connection: close, even though this should be default for 1.0, # because some servers misbehave w/o it. query = "%s /%s HTTP/1.0\r\nHost: %s\r\nConnection: close\r\nUser-Agent: compat\r\n\r\n" % ( method, path, host) try: yield from writer.awrite(query.encode('latin-1')) except: yield from writer.aclose() raise return reader
def print_http_headers(url): reader, writer = yield from asyncio.open_connection(url, 80) print(reader, writer) print("================") query = "GET / HTTP/1.0\r\n\r\n" yield from writer.awrite(query.encode('latin-1')) while True: line = yield from reader.readline() if not line: break if line: print(line.rstrip())
def request_raw(method, url): try: proto, dummy, host, path = url.split("/", 3) except ValueError: proto, dummy, host = url.split("/", 2) path = "" if proto != "http:": raise ValueError("Unsupported protocol: " + proto) reader, writer = yield from asyncio.open_connection(host, 80) # Use protocol 1.0, because 1.1 always allows to use chunked transfer-encoding # But explicitly set Connection: close, even though this should be default for 1.0, # because some servers misbehave w/o it. query = "%s /%s HTTP/1.0\r\nHost: %s\r\nConnection: close\r\nUser-Agent: compat\r\n\r\n" % (method, path, host) yield from writer.awrite(query.encode('latin-1')) # yield from writer.aclose() return reader
async def connect(self, clean_session=True): try: if self.connected: print("[Mqtt] Already connected!") return print("[Mqtt] Connecting...") self.reader, self.writer = await asyncio.wait_for( asyncio.open_connection(self.server, self.port), self.socket_timeout * 3) packet = self._create_connect_packet(clean_session) async with self.lock: await self._write(packet) resp = await self._read(4) if not (resp[0] == 0x20 and resp[1] == 0x02): raise OSError if resp[3] != 0: raise OSError self.connected = True print("[Mqtt] Connected!") except: print("[Mqtt] Connection failed!")
async def tcp_client(self): while True: fut = asyncio.open_connection(SERVER_IP, PORT) try: reader, writer = await asyncio.wait_for(fut, timeout=3) break except Exception as e: print("Timeout, retrying", e) while True: message = self.state writer.write(message.encode()) await writer.drain() data = await reader.read(100) other_state = data.decode() print(other_state) if other_state == "WON": self.on_loose() self.state = "LOOSE" elif other_state == "STARTED" and self.state != "STARTED" and self.state != "LOOSE": self.start() writer.close() await asyncio.sleep_ms(500)
async def ntrip_proc(): global header global server global port # Print info print('GSM connection started...\r\n') # Configure the GSM parameters nic.config(user='******', pwd='gprs', apn='internet', pin='1234') # Connect to the gsm network nic.connect(OnGsmStatus) # Wait till connection while not nic.isconnected(): await asyncio.sleep_ms(100) # Status info ifconfig = nic.ifconfig() print('GSM connection done: %s' % ifconfig[0]) # GSM info print('IMEI Number: %s' % nic.imei()) print('IMSI Number: %s' % nic.imsi()) qos = nic.qos() print('Signal Quality: %d,%d' % (qos[0], qos[1])) # Get the stream reader/writer while connwcto to the host reader, writer = yield from asyncio.open_connection(server, port) # Send header data to the host yield from writer.awrite(header) # Get data from the host try: data = yield from reader.read(256) print(data) except Exception as e: print('Not response from server!\r\n') try: while True: # Suspend for a while await asyncio.sleep_ms(1000) # There are some length bytes at the head here but it actually # seems more robust to simply let the higher level RTCMv3 parser # frame everything itself and bin the garbage as required. # Get the RTCM data from NTRIP caster data = yield from reader.read(2048) print(data) # Check the received data length if len(data) > 0 and not zed1.istxbusy(): # Redirect it to the destination zed1.send(data) except Exception as e: print('Not response from server!\r\n') finally: # Close the streams reader.aclose() writer.aclose() # Disconnect from the gsm network nic.disconnect()