-1

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.

2

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

my bad there was a typo on that, i typed correctly just have problem on it sending on python3, but thank you your help is highly appreciated
1

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

thank you so much for this answer, it is highly appreciated
i started getting this <built-in method recv of socket object at 0x006E2928>
I think you may have done response=client.recv and not response=client.recv(4096)
thats a typo i made in there

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.