0

I have to code a Python server which listen on the port 5000 and interpret data.

Here the code :

# -*- coding: utf-8 -*-
import binascii
import socket


UDP_IP = ""
UDP_PORT = 5000

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))

while true:
    data, addr = sock.recvfrom(150) 

Currently I can't interpret data, i try this funtion : binascii.b2a_uu() without result. Data in all frame have the same size, and are coded on 32 bits.

Thank you for your help.

Nicolas.

Edit : I use Python 2.7

lvc
  • 34,233
  • 10
  • 73
  • 98
NicoNB
  • 1
  • 1
  • Can you show example of data you receive from socket (just `print` output for example) and explain what do you want to convert it to? – Yaroslav Admin Jul 23 '15 at 13:40
  • If I print directly data on a terminal i got : (special caracters) or something like that ^A^@^@^@^?^@^@^A^@^@^@ if i write it on a file. I want to convert binary data to ASCII in order to retrieve messages (IP adress for example) send to my server. I can't use node.js, it's not implemented on the machine and i can't install it. – NicoNB Jul 23 '15 at 13:56
  • Okey, I see. Do you send data from another Python script and how do you encode them before sending? – Yaroslav Admin Jul 23 '15 at 14:05
  • No, data are send by an other script which encode them on 32 bits. I don't have more informations on how are sent informations. – NicoNB Jul 23 '15 at 14:18
  • If I understand you correctly, you receive only IP addresses. IP consists of four 8-bit numbers. To parse that in python you don't need to convert to ascii, you need to read 4 bytes from socket, convert it to int using [this](http://stackoverflow.com/questions/444591/convert-a-string-of-bytes-into-an-int-python), then using [this](http://stackoverflow.com/questions/9590965/convert-an-ip-string-to-a-number-and-vice-versa) convert number to IP string if needed. – Yaroslav Admin Jul 23 '15 at 14:57
  • If it's not the case, you need to understand how information are encoded to decode it. – Yaroslav Admin Jul 23 '15 at 15:07
  • Thank you for your help, this method works great in order to retireve IP adress. I know the order of information in data, and when i want to retrieve something else it doesn't work. For example, the first part of the data is 0C 00 00 00 (wireshark), the server write on a frame 0C (hexa) on a uint_32. How can i retrieve this information ? – NicoNB Jul 24 '15 at 08:25
  • Ok, i find a solution, with the method for retrieving IP adress, for 0C 00 00 00 the program print 201326592, with struct.unpack("L", )[0] for IP adress) it works ! Note that ">" or "<" mean big or Little endian. Thank you for your help !! – NicoNB Jul 24 '15 at 08:38
  • Good, that you solved your problem! Please, post solution as an answer to this question, so other users of SO can reuse your experience. – Yaroslav Admin Jul 24 '15 at 12:52

1 Answers1

0

Solution : After a recvfrom, I store data on a tab. Each information is coded on 32 bits, I read my table 4 bytes per 4 bytes and I use the function : struct.unpack(">L", data[0:4])[0] to retrive informations. Thid allow to have an IP address on long format

NicoNB
  • 1
  • 1