Python socket recv data parsing - python

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!

Related

Socket Python read bynary data

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

Connecting Teltonika device with Python over TCP

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'))

Python socket to send rows of data

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)

Python: using select with socket

I'm newbie in Python.
I wrote a socket server. It can read and send json-data. But I need to do it with select to extend it.
But I have no idea how to do it. I can't understand examples from the Internet - they have input(), sys.stdin and so on (for what??)
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('0.0.0.0', 8888,))
server_socket.listen(5)
while True:
client, address = server_socket.accept()
data = client.recv(1024)
print('received:', data.decode('utf-8'))
sending_data = bytes(generate_json(), 'utf-8')
print(sending_data)
client.send(sending_data)
client.close()

Sending data to multiple clients

Im creating a simple Client-Server chat system. Im using TCP and Im aware that TCP is end-end. But Im trying to forward the data that I receive from one client to the next client in array. But in the following code, Im trying to send it to all the clients(and even that fails :D)
Here is my code:
from socket import *
from os import *
from threading import *
def multipleClients():
all_clients = []
conn, addr = s.accept()
all_clients.append(conn)
print "is connected :D :)", addr
while True:
data= conn.recv(1024)
if not data:
break
print "message is :", repr(data)
for c in all_clients:
c.send(data)
host='localhost'
port=12000
s=socket(AF_INET, SOCK_STREAM)
s.bind((host, port))
s.listen(5)
print "Server is Running :D :p "
for i in range (5):
Thread(target=multipleClients).start()
TCP is a streaming protocol. You probably need some kind of message-protocol. recv can receive up to 1024 bytes, which can be one message, part of a message or multiple messages at once.
Every thread has only one client, because you have only one accept. You probably want to work with select to manage multiple connections.

Categories

Resources