#1
  1. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154

    Valid Statement?


    Can anyone tell me if this is a valid statement.
    s.send(chr(0xFF)+chr(BNETPacketId)+return struct.pack("< H", len(buffer)+4)+buffer)

    I wasn't sure because it has +return, I just want to know if that will work correctly.
    I also have another question, when using VB6 and you use the Left() function, if I do that (what python uses) would I always have to use - infront of my integer? Example,
    Left(text1, 1). I forgot how to do it in VB6 but I think you get the idea. In Python would it be like, text1[-1]? I just need to know if I MUST use the - integer.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    x(...) calls a function and returns the result.
    return is the command to send something out of a function as the result.

    Code:
    s.send("."+return struct.pack("< H", len(buffer)+4)+buffer)
    That would try to return exit the function you are in while also being in the middle of creating a string - Python will just complain and wont let you try to run it.

    If you just want "this string added to the result of this function" then you can do that:

    Code:
    s.send(chr(1) + chr(2) + struct.pack(...))


    I also have another question, when using VB6 and you use the Left() function, if I do that (what python uses) would I always have to use - infront of my integer? Example,
    Left(text1, 1). I forgot how to do it in VB6 but I think you get the idea. In Python would it be like, text1[:-1]? I just need to know if I MUST use the - integer.
    Nope. String/list slicing uses positive numbers to count from the left and negative characters to count from the right:

    text[startPosition:endPosition]
    Positive numbers step in from the left.
    Negative step in from the right.

    Code:
    >>> text = "abcde"
    >>> # take a single character
    >>> text[0]     # from the left
    'a'
    >>> text[2]     # somewhere in the middle
    'c'
    >>> text[-1]    # from the right
    'e'
    >>> # Take some characters
    >>> text[1:]     # from position 1 onwards - Right()
    'bcde'
    >>> text[:-1]   # from the far left ending 1 from the right - ??
    'abcd'
    >>> text[:3]     # from the left to position 3 - Left()
    'abc'
    >>> text[-2:]    # the last two characters
    'de'
    >>> text[2:4]    # from position 2 to position 4 - Mid()
    'cd'
    >>> text[2:-2]  # starting 2 from the far left, ending 2 in from the far right
    'c'
    >>>
    Slicing can also do more than this, should you wish:

    Code:
    >>> text[0:5:1]    # from the left (0) to the right (5) in steps of 1
    'abcde'
    >>> text[0:5:2]    # from 0 to 5 in steps of two
    'ace'
    >>> text[::3]    # from the left to the right in steps of three
    'ad'
    >>> text[::-1]    # well, try it and see.
    It's useful stuff, and far beats having named functions for each separate way of slicing a string up, IMHO.
    Last edited by sfb; December 16th, 2004 at 04:18 PM.
  4. #3
  5. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Ok, thanks I understand the second part much better now. I think I still have a problem with the first part however. In VB6 my code is, Winsock.SendData Chr(&HFF) & Chr(PacketId) & MakeWORD(Len(Buffer) + 4) & Buffer. In Python I have, s.send(chr(0xFF)+chr(BNETPacketId)+return struct.pack("< H", len(buffer)+4)+buffer). I'm not sure what you meant by what you said, but by doing +return struct.pack....... Will Python create some kind of an error? As of now it hasn't found anything wrong with it.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    Originally Posted by †Yegg†
    Will Python create some kind of an error? As of now it hasn't found anything wrong with it.
    What are you developing with? It does for me, in the interactive shell, in PythonWin's File -> Check, or when running Python script.py from the command line:

    Code:
    D:\>python test1.py
      File "test1.py", line 1
        s.send(chr(0xFF)+chr(BNETPacketId)+return struct.pack("< H", len(buffer)+4)+
    buffer)
                                                ^
    SyntaxError: invalid syntax
    
    D:\>
  8. #5
  9. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Ok, I've tried your way and I still got the invalid syntax error for the return function. Will my code function the same if I did the code in 2 lines? SOmething like,
    data = 0x08 # this isn't 1 of the 2 lines
    buffer = buffer+data
    return struct.pack("< I", data)

    Will this still work the same?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    I must admit I'm a bit lost... Let me explain the question as I am reading it and see if it makes any sense.

    In VB I have
    Code:
    Winsock.SendData Chr(&HFF) & Chr(PacketId) & MakeWORD(Len(Buffer) + 4) & Buffer
    From what I can find on Google, MakeWORD "Combines two bytes to one 2-byte Word (aka Integer). e.g.
    iRet = MakeWord(1, 1)"

    Your call only has one argument to MakeWORD though, and that's the length of the buffer, so I assume it pads it to two bytes with zeros if necessary.

    I guess that the data structure needed is:
    - A single byte of value 0xFF
    - A single byte Packet ID
    - Two bytes (a word) for the length of the following data (+4)
    - The buffer

    In python this would likely become:

    Code:
    socket.send(chr(0xFF) + chr(BNETPacketId) + struct.pack("<H", len(buffer) + 4) + buffer)
    (Assuming BNETPacketId is an integer between 0 and 255)



    return doesn't really come into this bit at all...


    Will my code function the same if I did the code in 2 lines? SOmething like,
    data = 0x08 # this isn't 1 of the 2 lines
    buffer = buffer+data
    return struct.pack("< I", data)
    data would become 0x08
    buffer would become buffer + data
    struct.pack would pad 0x08 into an unsigned long instead of an unsigned short
    The function would exit, and the value from struct.pack would be returned to the caller.
    Nothing would be sent anywhere and the changes just made to data and buffer would be lost.

    So... it wouldn't do the same thing as I think your earlier code does.

    I can see that you might mean something like this:
    Code:
    dataLength = struct.pack("<H", len(buffer)+4)
    header = chr(0xFF) + chr(BNETPacketId) + datalength
    
    message = header + buffer
    
    sendResult = s.send(message)
    return sendResult

    ?
  12. #7
  13. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Ooooooo. I didn't realize I didn't need return in there. But thanks for all the help I think this cleared things up a lot for me. And you're right about the PacketId. Thanks.

IMN logo majestic logo threadwatch logo seochat tools logo