Search code examples
pythonnetwork-programmingrequest

How to send requests to another computer with python


Basically I have a chatroom which I'm going to turn into a network (I know it doesn't sound like it makes a lot of sense) but basically I was wondering if I could have a python script capture all outgoing requests on a computer and instead send it to another computer (c2). I then want c2 to make the request on it's own. This is a watered down explanation of what I'm doing but any help will be great!


Solution

  • Firstly, you can set up a remote machine and get its IP address. On the remote machine you can set up this code:

    import socket
    
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = ('*CHANGE TO SERVER IP*', portnumber)
    print("Starting on %s port %s" % server_address)
    sock.bind(server_address)
    sock.listen(1)
    while True:
        connection, client_address = sock.accept()
        try:
            data = connection.recv(999)
            # You have received the data, do what you want with it.
        except:
            connection.close()
    

    And on the client machine:

    import socket
    
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = ('*INSERT SERVER IP*', portnumber)
    print('Connecting to %s port %s' % server_address)
    
    while True:
        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.connect(server_address)
            message=input('Message: ')
            if message=='quit':
                break
            sock.sendall(message)
        except:
            break
    sock.close()
    

    The server side code also works as client side for receiving information.