发送大量数据包而不是1个SSL Python

时间:2014-11-16 13:41:52

标签: python scapy modbus

我已经建立了一个实验,我通过SSL隧道传递Modbus流量(这是我在python中做过的第一件事)。我能够发送和接收数据,但是当我发送一个请求时,实际发送了大量请求(参见屏幕截图)

enter image description here

我尝试过多种配置,包括(在客户端和服务器中):

send()--no change
sendall() --no change
setblocking(1)
setblocking(0)--doesn't read all the data

在服务器端:
if data == Read_Coils_Answer - 我不认为我正在适当地转换大端进行比较而且这不起作用 while data: - while循环似乎是阻止任何一方停止使用" Broken Pipe"错误。所以这就是我正在使用的。

我最终计划使用for循环(现已注释掉并设置为4)。

我的服务器代码:

from ModLib import *
import socket, ssl, sys, pprint

try:
bindsocket = socket.socket()
bindsocket.bind(('', 502))
bindsocket.listen(5)
bindsocket.setblocking(1)

def do_something(connstream, data):
    readCoilsReq = str('\x01\x01\x00')
    answer = str(ModbusPDU01_Read_Coils_Answer)
    while data:
        print ("Request Recevied from Client:")
        print pprint.pformat(data)
        connstream.send(answer)
        print ("Answer Sent to Client")
        print pprint.pformat(answer)
    return False

def deal_with_client(connstream):
    data = connstream.recv(64)
    while data:
        if not do_something(connstream, data):
            break
        data = connstream.recv(64)

while True:
    newsocket, fromaddr = bindsocket.accept()
    connstream = ssl.wrap_socket(newsocket,
                         server_side=True,
                         certfile="server.crt",
                         keyfile="server.key",
                         ssl_version=ssl.PROTOCOL_TLSv1)
    try:
        deal_with_client(connstream)
    finally:
        connstream.shutdown(socket.SHUT_RDWR)
        connstream.close()
except KeyboardInterrupt:
print ("\nTerminating Session at User Request")
print ("No More Data Will be Sent/Recieved\n")
sys.exit(1)

我的客户端代码:

from ModLib import *
from time import sleep
import socket, ssl, pprint

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

ssl_sock = ssl.wrap_socket(s,
           ca_certs="server.crt",
           cert_reqs=ssl.CERT_REQUIRED)

ssl_sock.connect(('localhost', 502))
ssl_sock.setblocking(1)

readCoils = ModbusPDU01_Read_Coils()

#for i in range(4):
sleep(2)
ssl_sock.sendall(str(readCoils))
print ("Request for Read Coils Sent")

#start receive
data = ssl_sock.recv(64)
print ("Response from Server:")
print pprint.pformat(data)

if False: #from the python docs
    ssl_sock.write("""GET / HTTP/1.0\r
    Host: www.verisign.com\n\n""")

    data = ssl_sock.read()

    ssl_sock.close()

1 个答案:

答案 0 :(得分:1)

do_something()循环不是必需的,因为deal_with_client()循环正在做同样的事情。我删除了do_something()并将代码放在deal_with_client()中,这样我就可以保持连接打开(见下文)

from ModLib import *
import socket, ssl, sys, pprint

try:
    bindsocket = socket.socket()
    bindsocket.bind(('', 502))
    bindsocket.listen(5)
    bindsocket.setblocking(1)

    def deal_with_client(connstream):
        data = connstream.recv(1120)
        answer = str(ModbusPDU01_Read_Coils_Answer())
        while data:
            print ("Request Received from Client:")
            print pprint.pformat(data)
            connstream.send(answer)
            print ("Answer Sent to Client")
            print pprint.pformat(answer)
            data = connstream.recv(1120)

    while True:
        newsocket, fromaddr = bindsocket.accept()
        connstream = ssl.wrap_socket(newsocket,
                                     server_side=True,
                                     certfile="server.crt",
                                     keyfile="server.key",
                                    ssl_version=ssl.PROTOCOL_TLSv1)
        try:
            deal_with_client(connstream)
        finally:
            connstream.shutdown(socket.SHUT_RDWR)
            connstream.close()
except KeyboardInterrupt:
    print ("\nTerminating Session at User Request")
    print ("No More Data Will be Sent/Received\n")
    sys.exit(1)