Encoding.ASCII.GetBytes similar for Python ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rui

    Encoding.ASCII.GetBytes similar for Python ?

    Hi, how can i do what Encoding.ASCII. GetBytes (in .net, c#) does with
    the strings. I am trying to query some dns server to check its
    response using udp sockets. Some of the source below:

    # encoding: utf8
    import socket
    import sys
    import struct

    IP_PORT = 53
    server_host = ('4.2.2.1', IP_PORT)
    transaction_id = "Q1"
    TIMEOUT = 5

    type_string =
    "\u0001\u0000\u 0000\u0001\u000 0\u0000\u0000\u 0000\u0000\u000 0"
    trailer_string = "\u0000\u0000\u 0001\u0000\u000 1"

    address = 'google.com'
    url_name_start, domain_name = address.split(" .")

    # Query format copied from the C# example.
    #QueryString = TransactionID1 + TypeString + (char)URLNameSt art.Length
    + URLNameStart + (char)DomainNam e.Length + DomainName+ TrailerString;
    query = (transaction_id + type_string + str(len(url_nam e_start)) +
    url_name_start +
    str(len(domain_ name)) + domain_name + trailer_string)
    print query

    sock = socket.socket(s ocket.AF_INET, socket.SOCK_DGR AM)
    sock.settimeout (TIMEOUT)
    sock.connect(se rver_host)

    sock.send(query )
    data = sock.recv(512)

    for data_item in data:
    try:
    print chr(data_item)
    except:
    print data_item


    But it just returns trash:
    >python dns_checker.py
    Q1\u0001\u0000\ u0000\u0001\u00 00\u0000\u0000\ u0000\u0000\u00 006google3com
    \u0000\
    u0000\u0001\u00 00\u0001
    Q
    1
    Ï
    â—„






    Any advice ? Thanks!

  • Marc 'BlackJack' Rintsch

    #2
    Re: Encoding.ASCII. GetBytes similar for Python ?

    On Mon, 22 Sep 2008 04:23:09 -0700, Rui wrote:
    Hi, how can i do what Encoding.ASCII. GetBytes (in .net, c#) does with
    the strings. I am trying to query some dns server to check its response
    using udp sockets. Some of the source below:
    >
    # encoding: utf8
    import socket
    import sys
    import struct
    >
    IP_PORT = 53
    server_host = ('4.2.2.1', IP_PORT)
    transaction_id = "Q1"
    TIMEOUT = 5
    >
    type_string =
    "\u0001\u0000\u 0000\u0001\u000 0\u0000\u0000\u 0000\u0000\u000 0"
    trailer_string = "\u0000\u0000\u 0001\u0000\u000 1"
    Are you sure you want normal byte strings with *that* content!?

    In [90]: s = "\u0000\u0000\u 0001\u0000\u000 1"

    In [91]: len(s)
    Out[91]: 30

    In [92]: print s
    \u0000\u0000\u0 001\u0000\u0001

    This is not 5 unicode characters but 30 ASCII characters. But why using
    unicode anyway? I guess the DNS server expects bytes and not unicode
    characters.
    address = 'google.com'
    url_name_start, domain_name = address.split(" .")
    >
    # Query format copied from the C# example. #QueryString = TransactionID1
    + TypeString + (char)URLNameSt art.Length + URLNameStart +
    (char)DomainNam e.Length + DomainName+ TrailerString; query =
    (transaction_id + type_string + str(len(url_nam e_start)) +
    url_name_start +
    str(len(domain_ name)) + domain_name + trailer_string)
    print query
    >
    sock = socket.socket(s ocket.AF_INET, socket.SOCK_DGR AM)
    sock.settimeout (TIMEOUT)
    sock.connect(se rver_host)
    >
    sock.send(query )
    data = sock.recv(512)
    >
    for data_item in data:
    try:
    print chr(data_item)
    except:
    print data_item
    This will always end up in the ``except`` branch because `data` is a
    string and `chr()` expects integers. Any chance you wanted `ord()`
    instead?

    When you print out "trash" please use `repr()` so we can see what trash
    *exactly* you get.

    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    Working...