Python-Sockets:使用IF语句进行的评估行为错误

时间:2018-06-13 03:57:23

标签: python-2.7 sockets client-server tcp-ip python-sockets

Node.py

import socket
import sys
import time
import pp_json as ppjs
import SWATT as sw

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

#Get Client IP
client_IP = sock.getsockname()

#Status message for node
status_message = {'Node ID' : 'NS-1', 'Status' : 'Online', 'Node IP' : client_IP }

#Message serialized
ser_clientMsg = ppjs.pp_json(status_message)

#Start Verification variable initialization
flag = '101'


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


var=1
try:
    print >>sys.stderr, 'Sending Node Status "%s"' % ser_clientMsg
    sock.sendall(ser_clientMsg)
    # Send data
    while var:
        recvData = sock.recv(256)
        print >>sys.stderr, 'Received from Verifier: "%s" \n' % recvData
    if recvData == flag:
        print "Starting verification process...\n"
        filename = "hello.txt"
        fh = open(filename,"r")
        temp_str = fh.read()
        checksum = sw.SWATT(temp_str, len(temp_str))
        sock.sendall(checksum)
finally:
    print >>sys.stderr, 'closing socket'
    sock.close()

Verifier.py

import socket
import sys
import time as t
import json

startTime = t.time()

#Count variable initialization
count = 0

#Send Start Verification Prompt
start_verif = '101'

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

#Bind the socket to the port
server_address = ('localhost', 10100)
print >>sys.stderr, 'Starting Verifier on %s port %s' % server_address
sock.bind(server_address)

#Listen for incoming connections
sock.listen(1)

while True:
    #Wait for connection
    print >>sys.stderr, 'Waiting for connection establishment'
    connection, client_address = sock.accept()
    try:
        print >>sys.stderr, 'Connection from', client_address

        #Receive the data in chunks and retransmit it
        while True:
            if count == 0:
                statusData = connection.recv(256)
                parsedData = json.loads(statusData)
        #print >>sys.stderr, 'Received "%s"' % parsedData
                print "Received Node Status: \n", json.dumps(parsedData, indent=4, sort_keys=True)
                count+=1
            elif count == 1:
                print 'Sending Verification Trigger:\n', start_verif
                connection.sendall(start_verif)
                count+=1
                checksum = connection.recv(256)
                print "Checksum value from Prover: ", checksum
                print "\n"
            else:
                print >>sys.stderr, 'SWATT successfully computed on Node with Node address: ', client_address

    finally:
        elapsedTime = t.time() - startTime
        print elapsedTime
        connection.close()

我想这个问题出现在Node.py的第37行,因为我能够看到前一行打印的值101是节点终端上的第36行。你能帮我找到错误的确切位置吗?另外,如果您需要我导入的模块的代码,请发表评论。我很乐意随后发布。

1 个答案:

答案 0 :(得分:0)

while var:
    recvData = sock.recv(256)
    print >>sys.stderr, 'Received from Verifier: "%s" \n' % recvData
if recvData == flag:

接收到数据后,在检查其标志之前尚未对数据进行解码。在if语句之前尝试:recvData = recvData.decode('utf-8')

我并不是真的使用python 2,但是据我所知,这应该可以工作。