I am getting information from a socket via UDP.
I do not know how to read, this type of date below is binary, hexadecimal ???
��=~%01.00 EB200318151C0000003s��Z�t|
This is the result of part:
while True:
data, addr = sock.recvfrom(1024)
print "received message:", data
Can someone help, what kind of information is coming, how should I read it?
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
while True:
data, addr = s.recv(1024)
print "received message:" + data
Related
I'm trying to connect with Teltonika device (FMB1xx) with this code:
import socket
port = 12050
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', port))
s.listen(1)
conn, addr = s.accept()
print('Connected by ', addr)
imei = conn.recv(1024)
conn.send('\x01')
while True:
try:
data = conn.recv(1024)
if not data: break
print (data)
except socket.error:
print ("Error Occured.")
break
So far I've figured out that conn.send('\x01') doesn't work as it should, and device don't send the rest of data. There were a few questions like this, but none has a good answer. Here you can find documentation of this device.
It must be encoded and ordered (little-/big-endian) if you sending more then one byte. Use something like this:
conn.send(struct.pack('!L', 1))
About connecting to teltonika gps: https://github.com/Kein1945/GPS_Teltonika_Server/
like #uglymaxweber mentioned you have pack it as integer(four bytes) and on python3 you can use the built in to_bytes.
byteorder is little or big endian and the first parameter is the bytesize.
response = 5
conn.send(response.to_bytes(4, byteorder = 'big'))
this is my UDP-Server very according to the UDP-Server-Example from the python-wiki:
# ----- receiver.py -----
#!/usr/bin/env python
from socket import *
import sys
import select
host="192.168.88.51"
port = 1337
s = socket(AF_INET,SOCK_DGRAM)
s.bind((host,port))
addr = (host,port)
buf=128
data,addr = s.recvfrom(buf)
print "Received File:"
f = open("out.jpg",'wb')
data, addr = s.recvfrom(buf)
try:
while(data):
f.write(data)
s.settimeout(1)
data,addr = s.recvfrom(buf)
except timeout:
f.close()
s.close()
print "File Downloaded"
This code works fine and I'm able to receive ONE file at a time. But I have multiple clients and I'd like to receive every file coming in, so every time a new connection is established (from one certain IP).
Any suggestions?
First of all, if you want an asynchronous server, it's better to not write things from scratch with sockets. Instead, use packages like asyncio or Twisted.
Coming to your problem, it's more convenient to go with a TCP-focused socket, therefore you should use SOCK_STREAM instead of the UDP type SOCK_DGRAM.
First, define a function for downloading:
def get_file(s):
s.settimeout(1)
with open("out.jpg",'wb') as f:
data, addr = s.recv(buf)
try:
while(data):
f.write(data)
data, addr = s.recv(buf)
except timeout:
print "File Downloaded"
After setting up the constants (hostname, port number and so on), do something like the following (and do from threading import Thread first!):
s = socket(AF_INET,SOCK_DGRAM)
s.bind((host,port))
while True:
print "Waiting for connection..."
data, addr = s.recvfrom(buf)
print "... connection from:", addr
Thread(target=get_file, args=(s,)).start() #starts a new thread for file download, addr acts like a filename
I am trying to send image data from c++ to python using socket. The format of data sent from c++ is [data1,data2...] (includes ",")
When I print data received in python, it looks like this.
Print in python
I would like to parse and rearrange it to image array. How should I approach?
HOST, PORT = "localhost", 50007
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
for x in range(0, 1):
print("Step 1")
s.send(b'Hello')
print("Step 2")
data = s.recv(1000000)
print(data)
Thanks for help!
Greetings and apologies in advance if it looks a real novice question. I am new to python, or programming for that matter. I am writing a code that sends data from client to server. The data I need to send is from an csv file, which has around 10,000 rows. Currently I am sending the data in a large buffer as a whole, but I would prefer to send it row by row and also receive it the same way. I am not sure if I should use the split() function or there are any better ways to do the same thing.
The client...
import socket
HOST = 'server IP'
PORT = 42050
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
f = open('csvfile.csv', 'rb')
l = f.read(409600)
while True:
print l
s.send(l)
break
f.close()
print "Done Sending"
s.close()
The server...
import socket
HOST = 'local IP'
PORT = 42050
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
print "Server running", HOST, PORT
s.listen(5)
conn, addr = s.accept()
print'Connected by', addr
while True:
data = conn.recv(409600)
print repr(data)
if not data: break
print "Done Receiving"
conn.close()
Thanks in advance for the help.
im not sure what your question actually is ... but its pretty easy to send and receive lines
#client.py
for line in open("my.csv"):
s.send(line)
#server.py
def read_line(sock):
return "".join(iter(lambda:sock.recv(1),"\n"))
iter(lambda:sock.recv(1),"\n")
is essentially the same as
result = ""
while not result.endswith("\n"): result += sock.recv(1)
I just started coding in python I can't the encryption part of strings
I am trying to run this simple server client code
(The client is to run on raspberry-pi)
server :
#!/usr/bin/env python
import socket
server_socket = socket.socket()
server_socket.bind(('0.0.0.0', 8000))
server_socket.listen(0)
BUFFER_SIZE = 24
conn, addr = server_socket.accept()
print ('Got connection from', addr)
while 1:
data = conn.recv(BUFFER_SIZE)
if not data: break
print ("received data:", data)
conn.send(data) # echo
conn.close()
Client:(were I have the error)
import socket
client_socket = socket.socket()
client_socket.connect(("192.168.1.4", 8000))
BUFFER_SIZE = 1024
MESSAGE = "Hello, World!"
client_socket.send(MESSAGE)
data = client_socket.recv(BUFFER_SIZE)
client_socket.close()
print ("received data:", data)
The error here =:
File"c.py" line 9, in <module>
client_socket.send<MESSAGE>
typeError:'str' does not support the buffer interface
In python3 the interface to socket.send() changed to accept bytes instead of a string. See the difference between Python 3 docuentation and Python 2 documenation.
The solution is to encode the string before passing it to send() as follows:
client_socket.send(MESSAGE.encode())
In Python 3x strings are unicode, and they have to be encoded to bytes to send to a socket. The line:
client_socket.send(MESSAGE)
needs to be changed to:
client_socket.send(MESSAGE.encode('utf-8'))
On the server side you can decode data to get a string.