Beispiel #1
0
def execute_scenario(ip, port, message_f):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((ip, port))
    message = message_f()
    try:
        sock.sendall(message)
        response = sock.recv(BUFFER_SIZE)
        sys.stdout.write(response + "\n")
        sock.close()
    except socket.error, e:
        handle_socket_exception(e, sock)
def execute_scenario(ip, port, message_f):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((ip, port))
    message = message_f()
    try:
        sock.sendall(message)
        response = sock.recv(BUFFER_SIZE)
        sys.stdout.write(response + "\n")
        sock.close()
    except socket.error, e:
        handle_socket_exception(e, sock)
Beispiel #3
0
def file_scenario(ip, port, file_name):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((ip, port))
    try:
        script_dir = os.path.dirname(__file__)  # Absolute dir the script is in
        files_path = ".files/"

        abs_directory_path = os.path.join(script_dir, files_path)

        f = DistributedFile.open(file_name, abs_directory_path, sock)
        f.write("HELLO THERE!")
        f.close()
        sock.close()
    except socket.error, e:
        handle_socket_exception(e, sock)
def file_scenario(ip, port, file_name):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((ip, port))
    try:
        script_dir = os.path.dirname(__file__)  # Absolute dir the script is in
        files_path = ".files/"

        abs_directory_path = os.path.join(script_dir, files_path)

        f = DistributedFile.open(file_name, abs_directory_path,
                                 sock)
        f.write("HELLO THERE!")
        f.close()
        sock.close()
    except socket.error, e:
        handle_socket_exception(e, sock)
    def handle(self):
        semaphore = ThreadedTCPRequestHandler.semaphore
        handled = False  # Only acquire semaphore on first iteration

        my_num = ThreadedTCPRequestHandler.handler_num
        ThreadedTCPRequestHandler.handler_num += 1

        try:
            while True:
                if self.terminate_request:
                    raise TerminateRequestThread

                # self.request is the TCP socket connected to the client
                self.data = self.request.recv(BUFFER_SIZE)

                if not self.data:
                    break

                processing(self.data)

                # Attempt to acquire semaphore
                if not handled and not semaphore.acquire(DO_NOT_BLOCK):
                    refuse_connection(self)
                    return

                if is_kill_command(self.data):
                    kill_server()

                if begins_with_helo_text(self.data):
                    (host, port) = self.server.server_address
                    handle_helo_message(host, port, STUDENT_ID, self)
                    continue

                # Do work
                try:
                    self.handle_message()
                    handled = True
                except:
                    raise
        except socket.error, e:
            handle_socket_exception(e, self.request)