示例#1
0
    def handle(self, *args, **options):
        # Get node ID
        node_id = int(args[0])

        # Create socket
        client_socket = socket(AF_INET, SOCK_STREAM)
        # Connect to host on specified port
        client_socket.connect(('127.0.0.1', server_port()))

        # Try to contact server
        try:
            sendMsg(client_socket, str(node_id))
        except:
            abnormalShutdown(client_socket, "Error")

        # Shut down write side of socket
        client_socket.shutdown(1)

        # Receive incoming message from server
        try:
            response = recvMsg(client_socket)
        except:
            print "Could not receive message from server"

        # Close socket
        client_socket.close()
        return response
示例#2
0
def get_node_status(node_id):
    # Create socket
    clientSocket = socket(AF_INET, SOCK_STREAM)
    # Connect to localhost on specified port
    clientSocket.connect(('127.0.0.1', server_port()))
    # Print connection information to console
    #print("Connected to " + str(clientSocket.getpeername()[0]) + " on port " + str(server_port()))

    try:
        sendMsg(clientSocket, str(node_id))
    except:
        abnormalShutdown(clientSocket, "Error")
                    
    # Shut down write side of socket
    clientSocket.shutdown(1)
    
    # Receive incoming message from server
    return recvMsg(clientSocket)
    #print("Received from server " + str(incomingMsg))
    # Close socket
    clientSocket.close()
示例#3
0
文件: client.py 项目: slug62/sockets
    request = ""
    if type == 'E':
        request += type + str(content[0])
        for c in content[1]:
            request += ' ' + str(c)
    elif type == 'S':
        request += type + str(content[0]) + ' ' + str(content[1])
        for c in content[2]:
            request += ' ' + str(c)
        request += ' ' + str(content[3])
    else:
        print("You specified an incorrect reqeust type.")
    #print(request)
    return request

if __name__ == "__main__":
    a = 0
    b = 2
    poly = [-945, 1689, -950, 230, -25, 1]
    tol = 1e-15
    BISECTION = 'S'
    EVALUATE = 'E'

    bisection_result = sendtoserver('127.0.0.1', server_port(), buildrequest(BISECTION, [a, b, poly, tol]))
    # Bisection result will be the server response which will include a successful 'S' so my request below removes that
    # from the buildrequest
    evaluate_result = sendtoserver('127.0.0.1', server_port(), buildrequest(EVALUATE, [bisection_result[1:], poly]))

    print(bisection_result)
    print(evaluate_result)
__author__ = 'MichaelWomack'

from socket import socket
from server import server_port, encoding
from utility import formatData

if __name__ == "__main__":
    sock = socket()
    sock.connect(('127.0.0.1', server_port()))
    print("Connection made")

    a = 0
    b = 1
    poly = [-6, 14, -7, 1]
    tolerance = .01

    data = "S" + formatData([a, b, poly, tolerance])
    sock.sendall(str(data).encode(encoding))
    sock.shutdown(1)   # shutdown the sending side of the socket

    response_str = ""
    bytes = sock.recv(2048)
    while len(bytes) > 0:
        response_str += bytes.decode(encoding)
        bytes = sock.recv(2048)

    print("Response: ", response_str)
    sock.close()


    #reconnect socket