Untitled

mail@pastecode.io avatarunknown
python
2 months ago
1.6 kB
1
Indexable
Never
import http.client
import subprocess
import os
import sys

def send_request(connection, message):
    connection.request('POST', '/', message.encode())
    response = connection.getresponse()
    return response.read().decode()

if __name__ == '__main__':
    try:
        host_ip = sys.argv[1]
        host_port = int(sys.argv[2])

        # Create an HTTP connection to the server
        connection = http.client.HTTPConnection(host_ip, host_port)

        # Send initial message to establish connection
        send_request(connection, 'connect')

        while True:
            try:
                # Receive server message
                response = send_request(connection, '')
                print(response)

                if response == 'exit':
                    print('[-] The server has terminated the session.')
                    connection.close()
                    break

                # Get user input
                reply = input('Enter command: ')

                # Send command to server and get response
                response = send_request(connection, reply)
                print(response)

            except KeyboardInterrupt:
                print('\n[+] Keyboard interrupt issued.')
                send_request(connection, 'exit')
                connection.close()
                break

    except IndexError:
        print('[-] Command line argument(s) missing. Please provide host IP and port.')
    except ValueError:
        print('[-] Invalid port number. Please provide a valid integer port number.')
    except Exception as e:
        print(e)