• Welcome to Valhalla Legends Archive.
 

[Python]DataArrival

Started by Yegg, January 15, 2005, 02:33 PM

Previous topic - Next topic

Yegg

I can't seem to get the correct code for my bot's data arrival. I tried converting it from VB6 to Python, but I couldn't figure out the correct code.
Note: My bot is using chat client
Here is the VB6 code, and below that is what I've come up with.
Private Sub Winsock_DataArrival(ByVal bytesTotal As Long)
    Dim strTmp As String
    Winsock.GetData strTmp, vbString
    ParseData strTmp
End Sub

def dataArrival(data):
    tmpStr = s.recv(data)
    s.recv(tmpStr)
    parse_data(tmpStr)

Thanks.

j0k3r

This has nothing to do with java, why is it in the java forum.
QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo

Banana fanna fo fanna

ew...

BUFSIZE=1024
s = some socket
def readNextData():
    parse_data(s.recv(1024))


Of course, it looks to me like you might want to restructure your code a bit...

Yegg

Ok, I'm getting a problem with parse_data(s.recv(1024)). The bot connects to battle.net fine but it still can't receive any message, etc. I did a try and except to find out what was wrong and it had a problem with parse_data(s.recv(1024)). Right now I have parse_data = lambda d: map(handle_event, d.split("\r\n")). So I have no clue what's wrong. It seems like everything else is correct. So here's my code, I can't find anything wrong with it.

def event_user(args, string_arg):
    pass
def event_join(args, string_arg):
    pass
def event_leave(args, string_arg):
    pass
def event_recvwhisper(args, string_args):
    pass
def event_talk(args, string_args):
    pass
def event_broadcast(args, string_args):
    pass
def event_channel(args, string_args):
    print "You have joined a channel."
def event_flags(args, string_args):
    pass
def event_sendwhisper(args, string_args):
    pass
def event_channelfull(args, string_args):
    print "That channel is full."
def event_channelnotexist(args, string_args):
    print "That channel does not exist."
def event_channelrestricted(args, string_args):
    print "That channel is restricted."
def event_info(args, string_args):
    pass
def event_error(args, string_args):
    pass
def event_emote(args, string_args):
    pass
def event_name(args, string_args):
    pass
def event_unknown(args, string_args):
    print "An unknown error has occured."
event_handlers = {
    1001: event_user,
    1002: event_join,
    1003: event_leave,
    1004: event_recvwhisper,
    1005: event_talk,
    1006: event_broadcast,
    1007: event_channel,
    1009: event_flags,
    1010: event_sendwhisper,
    1013: event_channelfull,
    1014: event_channelnotexist,
    1015: event_channelrestricted,
    1016: event_info,
    1018: event_info,
    1019: event_error,
    1023: event_emote,
    2010: event_name,
    3000: event_info,
    }
def handle_event(d):
    try:
       if d.find('"') > -1:
           string_arg = d[d.find('"')+1:d.rfind('"')]
       else:
           string_arg = ""
       args = d.split(" ")
       event_handlers[int(args[0])](args, string_arg)
    except:
       event_unknown(d)
parse_data = lambda d: map(handle_event, d.split("\r\n"))


And after I connect I have parse_data(s.recv(1024)), so it looks like this,
s.connect((server, port))
Login(bnetuser, bnetpass)
parse_data(s.recv(1024))

Note: Login() just sends bnet the username and password, it's code is fine. Can you find anything wrong with this?

Banana fanna fo fanna

Put a bunch of print statements in there to see what it's doing.

Yegg

Ok, I used a bunch of try statements on all the places where an error could occur, here's my code and heres wut happens in the Interpreter.

def event_user(args, string_arg):
    pass
def event_join(args, string_arg):
    pass
def event_leave(args, string_arg):
    pass
def event_recvwhisper(args, string_args):
    pass
def event_talk(args, string_args):
    pass
def event_broadcast(args, string_args):
    pass
def event_channel(args, string_args):
    print "You have joined a channel."
def event_flags(args, string_args):
    pass
def event_sendwhisper(args, string_args):
    pass
def event_channelfull(args, string_args):
    print "That channel is full."
def event_channelnotexist(args, string_args):
    print "That channel does not exist."
def event_channelrestricted(args, string_args):
    print "That channel is restricted."
def event_info(args, string_args):
    pass
def event_error(args, string_args):
    pass
def event_emote(args, string_args):
    pass
def event_name(args, string_args):
    pass
def event_unknown(args, string_args):
    print "An unknown error has occured."
try:
    event_handlers = {
        1001: event_user,
        1002: event_join,
        1003: event_leave,
        1004: event_recvwhisper,
        1005: event_talk,
        1006: event_broadcast,
        1007: event_channel,
        1009: event_flags,
        1010: event_sendwhisper,
        1013: event_channelfull,
        1014: event_channelnotexist,
        1015: event_channelrestricted,
        1016: event_info,
        1018: event_info,
        1019: event_error,
        1023: event_emote,
        2010: event_name,
        3000: event_info,
        }
except:
    print "Problem: event_handlers"
def handle_event(d):
    try:
       if d.find('"') > -1:
           string_arg = d[d.find('"')+1:d.rfind('"')]
       else:
           string_arg = ""
       args = d.split(" ")
       event_handlers[int(args[0])](args, string_arg)
    except:
       event_unknown(d)
try:
    parse_data = lambda d: map(handle_event, d.split("\r\n"))
except:
    print "Problem: lambda"
def Login(username, password):
    s.send(chr(3) + chr(4) + username + "\r\n" + password + "\r\n")
print "["+mytime+"]Connecting to "+server+" "+"6112"
Log("["+mytime+"]Connecting to "+server+" "+"6112")
try:
    try:
        s.connect((server, port))
    except:
        print "Problem: s.connect((server, port))"
    try:
        Login(bnetuser, bnetpass)
    except:
        print "Problem: Login(bnetuser, bnetpass)"
    try:
        parse_data(s.recv(1024))
    except:
        print "Problem: parse_data(s.recv(1024))"
    print "["+mytime+"]Connect time: "+mytime
    Log("["+mytime+"]Connect time: "+mytime)
    print "["+mytime+"]Logged on as: "+bnetuser
    Log("["+mytime+"]Logged on as: "+bnetuser)
    print "["+mytime+"]Logon server: "+server+" 6112"
    Log("["+mytime+"]Logon server: "+server+" 6112")
except:
    print "["+mytime+"]Error: A connection error has occured, please check BG3."
    print "["+mytime+"]Error: You could also be ipbanned from battle.net."


Now heres wut coems up in the interpreter:
[4:08:43PM]Connecting to 127.0.0.1 6112
Problem: parse_data(s.recv(1024))
[4:08:43PM]Connect time: 4:08:43PM
[4:08:43PM]Logged on as: Yegg
[4:08:43PM]Logon server: 127.0.0.1 6112

Banana fanna fo fanna

Instead of printing "Problem: parse_data...", do this:

import traceback
traceback.print_exc()


Also, see
http://docs.python.org/lib/module-logging.html

Yegg

#7
:D. You own banana fanna fo fanna. Once I used that traceback thing I found 3 small errors and now my bot works great, it can see whos in the channel, who's joining/leaving, etc.
PS. I gave credit to you and 1 other person on my bot.

Update: I only have one problem. The program will not continue to parse data, it only does this once it connects, it shows the users in the channel, the info, the channel I connected to and the username I logged on with. Should i continuously send parse_data(s.recv(1024))?

Banana fanna fo fanna

You should infinitely loop it:


while True:
    parse_data(s.recv(1024))

Yegg

:D, thanks again, it works perfect.

Banana fanna fo fanna

any web page for your bot?

Yegg

Lol, actually, it started out as a private bot, but now that I realized that battle.net (my theory) is too slow to comprehend a floodbot spamming messages and someone else trying to ban/ignore it, because of this my bot wont ban floodbots, so I decided to just use it for logging on like 3 of my main accounts through the one bot until I can find a good use for it.