I'm learning ethical hacking with Python, and I have been trying to type a simple TCP client from BlackHat python book, but I have problems running the code I have written from the book.
import socket
target_host = "95.127.145.5"
target_post = 80
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((target_host,target_post))
client = send("GET / HTTP/1.1\r\nHost: 95.127.145.5\r\n\r\n")
response = client.recv(4096)
I'm not sure if it's because it's python2 but if it is, I need advice on how to convert this code to python3 because my IDE is python 3.8.3
the error happens in ("GET / HTTP/1.1\r\nHost: 95.127.145.5\r\n\r\n") and I get the message "inspect type checker options".
Any advice is highly appreciated.
-
check stackoverflow.com/questions/34192093/python-socket-getMeroz– Meroz2020年06月02日 20:37:51 +00:00Commented Jun 2, 2020 at 20:37
-
I get the message "inspect type checker options" Is that all the output?AMC– AMC2020年06月03日 01:12:42 +00:00Commented Jun 3, 2020 at 1:12
2 Answers 2
You have to encode the text before sending the request,
This line is also wrong
client = send("GET / HTTP/1.1\r\nHost: 95.127.145.5\r\n\r\n")
It should be :
client.send("GET / HTTP/1.1\r\nHost: 95.127.145.5\r\n\r\n")
See :
import socket
target_host = "95.127.145.5"
target_post = 80
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((target_host,target_post))
client.send("GET / HTTP/1.1\r\nHost: 95.127.145.5\r\n\r\n".encode())
response = client.recv(4096)
Also check whether the host is up or not
1 Comment
In python2,
client = send("GET / HTTP/1.1\r\nHost: 95.127.145.5\r\n\r\n")
should be
client.send("GET / HTTP/1.1\r\nHost: 95.127.145.5\r\n\r\n")
In python3, it just needs to be
client.send(b"GET / HTTP/1.1\r\nHost: 95.127.145.5\r\n\r\n")
edit: Roshin Raphel's answer does the encoding better than mine. And you're not using target_host when making up the request. So that line is probably better being
client.send(f"GET / HTTP/1.1\r\nHost: {target_host}\r\n\r\n".encode())
4 Comments
response=client.recv and not response=client.recv(4096)