TCP Server from Black Hat Python 2nd Edition (Issue with the client_handler = line) Any advice?

Joined
Oct 25, 2021
Messages
4
Reaction score
0
Ok so, I'm reading through Black Hat Python 2nd Edition, and it's excellent. I'm working on the TCP Server right now, but there's a problem in the code on the client_handler = line. It seems to have some sort of indentation or syntax issue, I'm not really sure which. It gives me the error Expected ")" Pylance [17,9]. I'm not really sure what it wants. Anyways, here's the code.

import socket import threading IP = '0.0.0.0' PORT = 9998 def main(): server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((IP, PORT)) (1) server.listen(5) (2) print(f'[*] Listening on {IP}:{PORT}') while True: client, address = server.accept() (3) print(f'[*] Accepted connection from {address[0]}:' '{address[1]})' client_handler = threading.Thread (target = handle_client, args = (client,)) client_handler.start() (4) def handle_client(client_socket): (5) with client_socket as sock: request = sock.recv(1024) print(f'[*] Received: {request.decode("utf-8")}') sock.send(b'ACK') if __name__ == '__main__': main()
 
Python:
import socket
import threading
IP = '0.0.0.0'
PORT = 9998
def main():
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.bind((IP, PORT))
    server.listen(5)
    print(f'[*] Listening on {IP}:{PORT}')
    while True:
        client, address = server.accept()
        print(f'[*] Accepted connection from {address[0]}:{address[1]}')
        client_handler = threading.Thread (target = handle_client, args = (client,))
        client_handler.start()


def handle_client(client_socket):
   with client_socket as sock:
        request = sock.recv(1024)
        print(f'[*] Received: {request.decode("utf-8")}')
        sock.send(b'ACK')
if __name__ == '__main__':
    main()

Output:
[*] Listening on 0.0.0.0:9998
[*] Accepted connection from 127.0.0.1:61805
[*] Received: test


Here you are. I'm not really sure why you put all these "(1)", "(2)" etc., so I deleted them, because they were are syntactically incorrect. The problem was Python is language where lines and indents matter, so you can't break them at any place like in java or other C++-like languages. I would suggest you to download PyCharm - it's a nice IDE that shows you exactly what is wrong with your code. I believe there is a free version of that.
 
Python:
import socket
import threading
IP = '0.0.0.0'
PORT = 9998
def main():
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.bind((IP, PORT))
    server.listen(5)
    print(f'[*] Listening on {IP}:{PORT}')
    while True:
        client, address = server.accept()
        print(f'[*] Accepted connection from {address[0]}:{address[1]}')
        client_handler = threading.Thread (target = handle_client, args = (client,))
        client_handler.start()


def handle_client(client_socket):
   with client_socket as sock:
        request = sock.recv(1024)
        print(f'[*] Received: {request.decode("utf-8")}')
        sock.send(b'ACK')
if __name__ == '__main__':
    main()

Output:
[*] Listening on 0.0.0.0:9998
[*] Accepted connection from 127.0.0.1:61805
[*] Received: test


Here you are. I'm not really sure why you put all these "(1)", "(2)" etc., so I deleted them, because they were are syntactically incorrect. The problem was Python is language where lines and indents matter, so you can't break them at any place like in java or other C++-like languages. I would suggest you to download PyCharm - it's a nice IDE that shows you exactly what is wrong with your code. I believe there is a free version of that.
Thank you! Eh well in the book it put those numbers by the lines, so I did too lol. At first, it had like 5 problems and 4 of them were the lines with those numbers. So when I put the parenthesis around them, the problems disappeared, so I figured it was solved. Then there was just the last problem on the client_handler line, which hopefully will be solved with your advice lol I'll let you know when I run it and see. And yes, Pycharm would probably be a very good idea at least until I learn the code language better. Thank you very much for your help!
 
Thank you! Eh well in the book it put those numbers by the lines, so I did too lol. At first, it had like 5 problems and 4 of them were the lines with those numbers. So when I put the parenthesis around them, the problems disappeared, so I figured it was solved. Then there was just the last problem on the client_handler line, which hopefully will be solved with your advice lol I'll let you know when I run it and see. And yes, Pycharm would probably be a very good idea at least until I learn the code language better. Thank you very much for your help!
It worked! Thank you.
 
You are welcome, I'm glad I could help. By the way, "hacking" is really hard piece of cake, believe me, I know what I'm saying. It takes massive efford and time to learn it well. So I wish you good luck and don't give up! :)
 
You are welcome, I'm glad I could help. By the way, "hacking" is really hard piece of cake, believe me, I know what I'm saying. It takes massive efford and time to learn it well. So I wish you good luck and don't give up! :)
Thanks! I consider it the ultimate intellectual challenge. Keeps me sharp! :)
 
Back
Top