Why python asyncio coroutine is pausing at the line socket.recv(1024) ? why the while loops are failed to cross yeild statements? - asynchronous

I have a small application where, one coroutine will send data and the other coroutine will receive data and logs whether it received the exact transmitted data or not.
Both coroutines are in while loops. Some how, the trans() coroutine and recv() coroutine are not proceeding ahead when they hit the line yield from XXXXXXX
data, server = yield from recv_sock.recvfrom(1024)
Here is the code
import asyncio
import socket
import time
import datetime
import logging
trans_addr = ('localhost', 5555)
recv_addr = ('localhost',6666)
#asyncio.coroutine
def trans():
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(trans_addr)
i = 0
global sen_data
while True:
print("hi")
sen_data = "HELLO " + str(i)
sent = yield from sock.sendto(sen_data.encode(), recv_addr)
print(sent)
print("hi1")
yield from time.sleep(2)
i += 1
print("hi1")
#asyncio.coroutine
def recv():
recv_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
recv_sock.bind(recv_addr) # binding the receiving end to 1.241 and port 6666
#recv_sock.setblocking(0)
while True:
try:
print("hello")
data, server = yield from recv_sock.recvfrom(1024)
print("hello2")
if (data):
recv_data = data.decode()
if (sen_data == recv_data):
logging.info("transmitted data :" + sen_data + " is Received as :" + recv_data + " at :" + str(
datetime.datetime.now()) + '\n')
print("transmitted data :" + sen_data + " is Received as :" + recv_data + " at :" + str(
datetime.datetime.now()) + " from :" + str(server) + '\n')
else:
logging.critical("Data missed : ")
logging.critical("Transmitted data " + sen_data + " is != " + "received data : " + recv_data + '\n')
print("data is missing--->")
print("Transmitted data " + sen_data + " is != " + "received data : " + recv_data + '\n')
except:
pass
# print("not receiving data due to some fault in the receiving socket")
# time.sleep(1)
loop=asyncio.get_event_loop()
tasks = [loop.create_task(trans()), loop.create_task(recv())]
wait_tasks = asyncio.wait(tasks)
loop.run_forever()
loop.run_until_complete(wait_tasks)
Output is:
hello
hi
Can anyone let me know,why the coroutines are failed to cross the yield from commands? i am using python 3.3.2

yield from or await should be used with coroutines. recvfrom is not a coroutine. For example, you can use loop.sock_recv() instead:
reader, writer = socket.socketpair()
writer.setblocking(False)
reader.setblocking(False)
await loop.sock_recv(...)
await loop.sock_sendall(...)

Related

How to get a message from a Chat, after using a command and storing that message as a variable (Pyrogram)?

So I am kinda new with Pyrogram and I want to create my own Genshin Bot. After using the command redeem code, I want the message to be taken and stored as variable. so can anyone help me with that
after taking the code as input from user I would be able to use genshin.py api wrapper to redeem code. Just need help with getting message and storing it as variable.
import genshin
import os
from dotenv import load_dotenv
from pyrogram import Client, filters
load_dotenv()
global chatid
chatid = 842544591
global uid
uid = os.getenv("uid")
ltuid = os.getenv("ltuid")
ltoken = os.getenv("ltoken")
cookie_token = os.getenv("cookie_token")
api_id = os.getenv("api_id")
api_hash = os.getenv("api_hash")
bot_token = os.getenv("bot_token")
cookies = {"ltuid": ltuid,
"ltoken": ltoken,
"cookie_token": cookie_token,
"uid": uid}
client = genshin.Client(cookies)
bot = Client(
"Genshin Bot",
api_id=api_id,
api_hash=api_hash,
bot_token=bot_token
)
#bot.on_message(filters.command('start'))
def start_command(bot, message):
message.reply_text(
"Welcome to Genshin Auto Tasks Bot.\nFor Getting Started Use /help command.")
#bot.on_message(filters.command('help'))
def help_command(bot, message):
message.reply_text("This is Bot's Help Section")
#bot.on_message(filters.command('notes'))
async def get_notes(bot, message):
data = await client.get_full_genshin_user(uid)
notes = await client.get_notes(uid)
active_days = (data.stats.days_active)
total_characters = (data.stats.characters)
abyss_total_stars = (data.abyss.previous.total_stars)
resin_count = notes.current_resin
resin_recovery_time = notes.remaining_resin_recovery_time
await message.reply_text("Pranay Asia" + "\n" +
"uid : " + str(uid) + "\n" +
"-----------------------------------------------------------------" + "\n" +
"Resin Count: " + str(resin_count) + "/" + str(notes.max_resin) + "\n" +
"Countdown to next resin recovery: " + str(resin_recovery_time) + "\n" +
"Total No. of Active Days: " + str(active_days) + "\n" +
"Total No. of Characters: " + str(total_characters) + "\n" +
"Total Stars in Abyss: " + str(abyss_total_stars)
)
#bot.on_message(filters.command('redeemcode'))
def redeem_code(bot, message):
message.reply_text("Send the Code to Redeem")
bot.run()
try message.text
I use it as a userbot but nothing changes so much. This piece of code saves the sent message to a variable and filters out the command itself. For a bot it will be easier: answer = message.text
#app.on_message(filters.command("ns", prefixes=".") & filters.text)
async def EXAMPLE(_,msg):
orig_text = msg.text.split(".ns ", maxsplit=1)[1]
text = orig_text

Searching throughout telnet output result while doing parallel telnet to multiple network devices

I am trying to find a string (e.g.: ZNTS) from telnet output to a host.
I want to telnet to multiple hosts at the same time because waiting to finish from host to host takes a long time. The host can not response fast enough, so I need to sleep after every command line.
Below is my code:
import sys
import telnetlib
import time
MXK_LIST = ['172.16.32.15',
'172.16.33.30',
'192.168.55.3',
'192.168.52.3',
'192.168.54.3',
'192.168.42.25',
'192.168.43.3',
'192.168.44.3',
'192.168.45.3',
'192.168.46.3',
'192.168.47.3',
'192.168.48.3',
'192.168.49.9']
TOTAL_MXK = len(MXK_LIST)
ZNTS = str(sys.argv[1])
DEBUG = str(sys.argv[2])
username = "admin"
password = "4n0cmek0n9net"
I = 0
C = 0
print "\n Finding ZNTS = " + ZNTS
print "\n It will take around 5 minutes to complete the searching !!\n"
for MXK in MXK_LIST:
I = I + 1
OUTPUT = ""
try:
tn = telnetlib.Telnet(MXK)
except:
print "Bad Connection. Please verify IP again."
sys.exit(0)
tn.read_until(b"login: ",1)
tn.write(username + "\n")
time.sleep(5)
tn.read_until(b"password: ",1)
tn.write(password + "\n")
time.sleep(5)
OUTPUT = tn.read_until(b">",1)
if DEBUG == 1: print OUTPUT
time.sleep(5)
tn.write("onu showall 1" + "\n")
time.sleep(5)
tn.read_until(b"[no] ",1)
tn.write(b"yes" + "\n")
time.sleep(15)
OUTPUT = tn.read_until(b"quit",1)
time.sleep(5)
if DEBUG == 1: print OUTPUT
tn.write("A" + "\n")
time.sleep(5)
OUTPUT = tn.read_until(b">",1)
if DEBUG == 1: print OUTPUT
tn.write("exit\n")
if ZNTS in OUTPUT:
print str(I) + "/" + str(TOTAL_MXK) + " : FOUND " + ZNTS + " IN " + MXK
C = C + 1
else:
print str(I) + "/" + str(TOTAL_MXK) + " : NOT FOUND " + ZNTS + " IN " + MXK
print "\n"+ZNTS + " is found in " + str(C) + " MXK"
You can try to spawn a thread for each telnet session and parallelize the search that way.
Check out the multi-thread sample code here:
http://stackoverflow.com/questions/6286235/multiple-threads-in-python

Log Parser c# error using STRCAT with CASE

I'm having trouble with the log parser, punctually on the use of the function STRCAT parameter with CASE, using log parser the query works perfectly and using a simple STRCAT without CASE the query works even using c#, the problem starts when i use CASE. Am I missing something?
Here's the error:
CLogQueryClass: Error 8007064f: Execute: error parsing query: Syntax Error: : cannot find closing parenthesys for function STRCAT [ SQL query syntax invalid or unsupported. ]
string query = "SELECT " + " STRCAT('" + entry.Name +"'";
query += #", CASE INDEX_OF(SUBSTR(cs-uri-stem,1), '/')
WHEN 'NULL' THEN 'DEFAULTAPPPOOL'
ELSE EXTRACT_TOKEN(cs-uri-stem,1,'/')
END";
query += ") AS APPPOOL";
query += ", '" + Environment.MachineName + "' as server";
query += ", '" + entry.Name + "' as site";
query += ", cs-uri-stem as csUriStem";
query += ", c-ip as cIp, sc-status as scStatus";
query += ", sc-bytes as scBytes";
query += ", cs-bytes as csBytes";
query += ", time-taken as timeTaken";
query += " FROM " + logAddress + "\\" + yesterdayLogName;
// Initialize a LogQuery object
logQuery = new LogQueryClass();
logRecordSet = logQuery.Execute(query,new COMIISW3CInputContextClass());
//SHOWS RESULT
for (; !logRecordSet.atEnd(); logRecordSet.moveNext())
{
logrecord = logRecordSet.getRecord();
int i = 0;
while (i < 9)
{
Console.WriteLine(logrecord.getValue(i));
i++;
}
Thanks
First, it looks like you are mixing types. The CASE INDEX_OF(SUBSTR(cs-uri-stem,1), '/') WHEN 'NULL' compares an integer to string. This should be:
CASE INDEX_OF(SUBSTR(cs-uri-stem,1), '/')
WHEN NULL THEN 'DEFAULTAPPPOOL'
ELSE EXTRACT_TOKEN(cs-uri-stem,1,'/')
END
The error complains about finding the close parenthesis, but I've found that parsing errors can result in misleading error messages with LogParser.
Second, I've tested the following in C# targeted at .NET 3.5 (4.0 had an issue with embedded type. Similar to this...):
string logAddress = "C:\\Path\\to\\consolidatedFile";
string entryName = "blah";
string yesterdayLogName = "fileName.log";
string query = "SELECT " + " STRCAT('" + entryName + "'"
+ ", CASE INDEX_OF(SUBSTR(cs-uri-stem,1), '/') "
+ "WHEN NULL THEN 'DEFAULTAPPPOOL' "
+ "ELSE EXTRACT_TOKEN(cs-uri-stem,1,'/') "
+ "END"
+ ") AS APPPOOL"
+ ", '" + Environment.MachineName + "' as server"
+ ", '" + entryName + "' as site"
+ ", cs-uri-stem as csUriStem"
+ ", c-ip as cIp, sc-status as scStatus"
+ ", sc-bytes as scBytes"
+ ", cs-bytes as csBytes"
+ ", time-taken as timeTaken"
+ " FROM " + logAddress + "\\" + yesterdayLogName;
// Initialize a LogQuery object
COMW3CInputContextClassClass ctx = new COMW3CInputContextClassClass();
//LogQueryClass logQuery = new LogQueryClass();
LogQueryClass logQuery = new LogQueryClassClass();
//ILogRecordset logRecordSet = logQuery.Execute(query, new COMIISW3CInputContextClass());
ILogRecordset logRecordSet = logQuery.Execute(query, ctx);
//SHOWS RESULT
for (; !logRecordSet.atEnd(); logRecordSet.moveNext())
{
ILogRecord logrecord = logRecordSet.getRecord();
int i = 0;
while (i < 9)
{
Console.WriteLine(logrecord.getValue(i));
i++;
}
}
This ran successfully and return results. I commented out the lines initially presented since when I used them nothing returned on the console. That might be a difference of the code not presented. Finally, I substituted a string entryName for the entry.Name object assuming that it returns a string.
I hope this helps.

Python: infinite loop while executing SQLite statement

I have this 2 SQLite scripts:
both tested by direct input into SQLite.
def getOutgoingLinks(self, hostname):
t = (hostname,)
result = self.__cursor.execute("SELECT url.id, hostname.name, url.path, linking_to.keyword, siteId.id " +
"FROM url, hostname, linking_to, " +
"(SELECT url.id FROM url, hostname " +
"WHERE hostname.name = (?) " +
"AND hostname.id = url.hostname_id " +
") AS siteId " +
"WHERE linking_to.from_id = siteId.id " +
"AND linking_to.to_id = url.id " +
"AND url.hostname_id = hostname.id", t)
result = result.fetchall()
return result
def getIncommingLinks(self, hostname):
t = (hostname,)
result = self.__cursor.execute("SELECT url.id, hostname.name, url.path, linking_to.keyword, siteId.id " +
"FROM url, hostname, linking_to, " +
"(SELECT url.id FROM url, hostname " +
"WHERE hostname.name = (?) " +
"AND hostname.id = url.hostname_id " +
") AS siteId " +
"WHERE linking_to.to_id = siteId.id " +
"AND linking_to.from_id = url.id " +
"AND url.hostname_id = hostname.id", t)
result = result.fetchall()
return result
The getIncommingLinks() methond works very well, but getOutgoingLinks() causes an infinite Loop when python trys to execute the SQL statement. Any ideas what went wrong?
Rewrite your Select statements without select ...( Select ...) - thats very bad style. The result may solve your problem.
If by infinite loop you mean that the function doesnt ever get to return a value, I had the same problem.
Found the solution in Why python+sqlite3 is extremely slow?. With large tables it becomes a performance issue with the version shipped with python 2.7. I solved it by upgrading sqlite3 as specified here: https://stackoverflow.com/a/3341117/3894804

Monitor a programs data transfer

Is there a way to monitor what data a program sends and receives from the internet? So for example if you have a game and it takes some information from a server, since the program would have to be told the information, is there a way of recording that and reading it? I've read a few questions similar but they were all focused on how much data was transferred. I what to know what data is being transferred
You're looking for Fiddler (for HTTP traffic) or Wireshark (for all traffic).
extView infoView = (TextView)findViewById(R.id.traffic_info);
String info = "";
info += "Mobile Interface:\n";
info += ("\tReceived: " + TrafficStats.getMobileRxBytes() + " bytes / " + TrafficStats.getMobileRxPackets() + " packets\n");
info += ("\tTransmitted: " + TrafficStats.getMobileTxBytes() + " bytes / " + TrafficStats.getMobileTxPackets() + " packets\n");
info += "All Network Interface:\n";
info += ("\tReceived: " + TrafficStats.getTotalRxBytes() + " bytes / " + TrafficStats.getTotalRxPackets() + " packets\n");
info += ("\tTransmitted: " + TrafficStats.getTotalTxBytes() + " bytes / " + TrafficStats.getTotalTxPackets() + " packets\n");
infoView.setText(info);
mStartRX = TrafficStats.getTotalRxBytes();
mStartTX = TrafficStats.getTotalTxBytes(); TextView RX = (TextView)findViewById(R.id.RX);
TextView TX = (TextView)findViewById(R.id.TX);
long rxBytes = TrafficStats.getTotalRxBytes()- mStartRX;
RX.setText(Long.toString(rxBytes));
long txBytes = TrafficStats.getTotalTxBytes()- mStartTX;
TX.setText(Long.toString(txBytes));

Resources