-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.

Tonechas
  • 13,398
  • 16
  • 46
  • 80

2 Answers2

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

Roshin Raphel
  • 2,612
  • 4
  • 22
  • 40
  • 2
    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 – snaggingjoker45 Jun 02 '20 at 20:55
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())
Finlay McWalter
  • 1,222
  • 1
  • 10
  • 12