473,287 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Encoding.ASCII.GetBytes similar for Python ?

Rui
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\u0000\u0001\u0000\u0000\u0000\u0000\ u0000\u0000"
trailer_string = "\u0000\u0000\u0001\u0000\u0001"

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

# Query format copied from the C# example.
#QueryString = TransactionID1 + TypeString + (char)URLNameStart.Length
+ URLNameStart + (char)DomainName.Length + DomainName+ TrailerString;
query = (transaction_id + type_string + str(len(url_name_start)) +
url_name_start +
str(len(domain_name)) + domain_name + trailer_string)
print query

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(TIMEOUT)
sock.connect(server_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\u0000\u0000\u0000\u0000 \u0000\u00006google3com
\u0000\
u0000\u0001\u0000\u0001
Q
1
Ï



Any advice ? Thanks!

Sep 22 '08 #1
1 5549
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\u0000\u0001\u0000\u0000\u0000\u0000\ u0000\u0000"
trailer_string = "\u0000\u0000\u0001\u0000\u0001"
Are you sure you want normal byte strings with *that* content!?

In [90]: s = "\u0000\u0000\u0001\u0000\u0001"

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

In [92]: print s
\u0000\u0000\u0001\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)URLNameStart.Length + URLNameStart +
(char)DomainName.Length + DomainName+ TrailerString; query =
(transaction_id + type_string + str(len(url_name_start)) +
url_name_start +
str(len(domain_name)) + domain_name + trailer_string)
print query

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(TIMEOUT)
sock.connect(server_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
Sep 22 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Sebastian Meyer | last post by:
Hi newsgroup, i am trying to replace german special characters in strings like str = re.sub('', 'oe', str) When i work with this, i always get the message UniCode Error: ASCII decoding error...
10
by: Christopher H. Laco | last post by:
Long story longer. I need to get web user input into a backend system that a) only grocks single byte encoding, b) expectes the data transer to be 1 bytes = 1 character, and c) uses the HP Roman-6...
4
by: Curt Fluegel | last post by:
I seem to be having a problem base64 encoding characters above 127. I can encode a sentence like "The big bad dog" without problems, but if I try to encode something like 0xFF I get different...
13
by: Dan V. | last post by:
How do I create a one line text file with these control codes? e.g.: 144 = 0x90 and 147 = 0x93? I am trying to create a one line text file with these characters all one one row with no spaces. ...
10
by: Marc Jennings | last post by:
Hi there, Can anyone point out any really obvious flaws in the methodology below to determine the likely encoding of a file, please? I know the number of types of encoding is small, but that is...
1
by: Emilio | last post by:
Question about Dim data As () = System.Text.Encoding.ASCII.GetBytes(message) Would it be the same to write: Dim data() As Byte = System.Text.Encoding.ASCII.GetBytes(message) ?
5
by: Peter Romero | last post by:
All I want to do is write a simple app that takes a user-entered character and gives me the 1, 2, or 4 bytes that represent it in UTF-8. Common sense indicates that the active code should be about...
1
by: devgrt | last post by:
Checking if proper/best way to do this... I have to pass a C# string to a C++ DLL. The function in the DLL (gethostbyname) requires a null terminated string. It seems like when you do byte var =...
9
by: =?Utf-8?B?RGFu?= | last post by:
I have the following code section that I thought would strip out all the non-ascii characters from a string after decoding it. Unfortunately the non-ascii characters are still in the string....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.