使用python和ctype结构通过套接字发送/接收数据

yftpprvb  于 2023-01-12  发布在  Python
关注(0)|答案(2)|浏览(181)

我尝试在python客户端和服务器端之间发送和接收C结构数据,客户端和服务器端在连接和交换数据方面都很顺利,客户端向服务器端发送一个ctype结构,服务器端再将其发送回来,问题是我不知道如何解释客户端接收到的消息,并按照结构格式提取数据。我的最后一个想法是让python服务器和C客户机相互对话,并在预定义的结构中交换数据。
下面是我用Python编写的客户端和服务器代码:

客户代码

import socket
import sys
import time

from ctypes import *

class payload_t(Structure):
    _fields_ = [("ms", c_ulong),
                ("counter", c_ulong),
                ("DHT_temperature", c_float),
                ("DHT_humidity", c_float),
                ("DS_temperature", c_float),
                ("temperature_setpoint", c_float),
                ("time_setpoint", c_float)]

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect the socket to the port where the server is listening
server_address = ('localhost', 10000)
print >>sys.stderr, 'connecting to %s port %s' % server_address
sock.connect(server_address)

try:
    for i in range(0,10):
        # Send data
        payload=payload_t(i*1000,i+1,25.2,45.7,25.8,33.22,3600.0)
        message = payload
        # 'This is the message.  It will be repeated.'
        print 'length of message %d' % sizeof(message)
        print 'sending "', message.ms, message.counter, message.DHT_temperature, message.DHT_humidity, message.DS_temperature, message.temperature_setpoint, message.time_setpoint, '"'
        sock.sendall(message)
        # time.sleep(0.1)
        # Look for the response
        amount_received = 0
        amount_expected = sizeof(message)

        while amount_received < amount_expected:
            datap = sock.recv(sizeof(message))
            amount_received += len(datap)
        print >>sys.stderr, 'received "%s"' % datap
        print type(datap)
        payload=payload_t()
        datap.readinto(payload)
        data=datap.readinto(payload_t)
        data=struct.unpack(payload_t,datap)
            print 'Received "', data.ms, data.counter, data.DHT_temperature, data.DHT_humidity, data.DS_temperature, data.temperature_setpoint, data.time_setpoint, '"'

finally:
    print >>sys.stderr, 'closing socket'
    sock.close()

服务器代码

import socket
import sys
from ctypes import *

class payload_t(Structure):
    _fields_ = [("ms", c_ulong),
                ("counter", c_ulong),
                ("DHT_temperature", c_float),
                ("DHT_humidity", c_float),
                ("DS_temperature", c_float),
                ("temperature_setpoint", c_float),
                ("time_setpoint", c_float)]

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to the port
server_address = ('localhost', 10000)
print >>sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)

# Listen for incoming connections
sock.listen(1)
payload=payload_t(0,0,0.0,0.0,0.0,0.0,0.0)
while True:
    # Wait for a connection
    print >>sys.stderr, 'waiting for a connection'
    connection, client_address = sock.accept()

    try:
        print >>sys.stderr, 'connection from', client_address

        # Receive the data in small chunks and retransmit it
        while True:
            data = connection.recv(sizeof(payload))
            print >>sys.stderr, 'received "%s"' % data
            if data:
                print >>sys.stderr, 'sending data back to the client'
                connection.sendall(data)
            else:
                print >>sys.stderr, 'no more data from', client_address
                break

    finally:
        # Clean up the connection
        connection.close()
9lowa7mx

9lowa7mx1#

首先,您当前正在转储接收到的数据,在每个循环迭代中重新分配变量“datap”。
你可以这样做:

datap += sock.recv(sizeof(message) - amount_received)

(Of当然,这不是最有效的代码,但您已经明白了。)
当你在“datap”变量中组装了你的结构后,你可以使用“from_buffer”或“from_buffer_copy”方法将它加载到你的ctype类中。

payload = payload_t.from_buffer_copy(datap)

在您的情况下,后者更好,因为当您重新绑定'datap' var时,缓冲区可能会消失。

tgabmvqs

tgabmvqs2#

直接用recv_into调用提供ctypes.Structure的示例怎么样?
下面是服务器代码的修改版本,希望我没有删除您希望保留的功能

import socket
import sys
from ctypes import *

class payload_t(Structure):
    _fields_ = [("ms", c_ulong),
                ("counter", c_ulong),
                ("DHT_temperature", c_float),
                ("DHT_humidity", c_float),
                ("DS_temperature", c_float),
                ("temperature_setpoint", c_float),
                ("time_setpoint", c_float)]

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to the port
server_address = ('localhost', 10000)
print >>sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)

# Listen for incoming connections
sock.listen(1)
payload=payload_t()
while True:
    # Wait for a connection
    print >>sys.stderr, 'waiting for a connection'
    connection, client_address = sock.accept()

    try:
        print >>sys.stderr, 'connection from', client_address

        # Receive the data in small chunks and retransmit it
        while True:
            data = connection.recv_into(payload)
            print >>sys.stderr, 'received "%s"' % data
            if data:
                print >>sys.stderr, 'sending data back to the client'
                print >>sys.stderr, 'time_setpoint "%f"' % payload.time_setpoint
                connection.sendall(payload)
            else:
                print >>sys.stderr, 'no more data from', client_address
                break

    finally:
        # Clean up the connection
        connection.close()

因此,基本上,我所做的唯一事情就是使用socket.recv_into调用直接进入结构的示例:http:docs.python.org/3/library/socket.html#socket.recv_into
这使我可以在以后直接访问结构属性。
我猜这个结构足够小(28个八位字节),所以数据可以放在一个recv_into调用中,如果你需要多次调用recv_into来构建完整的结构,那么......
如果你有python3.7,你可以用切片内存视图对你的结构进行多次调用:

mv = memoryview(payload).cast('B')
size = sizeof(payload)
while size > 0:
    data = connection.recv_into(mv) 
    mv = mv[data:] 
    size -= data

没有什么东西能保证是诚实的。在Python的早期版本中,当你试图在memoryview上强制转换'B'时,你可能会遇到异常。回想一下ctypes.Structure上的memoryview是0-dim的,如果没有强制转换,就几乎不可能对它进行切片...

相关问题