I have an error when I create a simple TCP client:
Exception has occurred: TypeError a bytes-like object is required, not 'str' in line client.send("GET / HTTP/1.1\r\nHost: google.com\r\n\r\n")
My Python version is 3.8.
import socket
target_host = "www.google.com"
target_port = 80
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((target_host, target_port))
client.send("GET / HTTP/1.1\r\nHost: google.com\r\n\r\n")
response = client.recv(4096)
print(response)
Related
i can recv info from client but i dont know how to recv it from server.
"i think in line 15 i need to change "socket" to somethong"
it must be that type: 'socket.socket'
client:
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # AF_INET = IP, SOCK_STREAM = TCP
client.connect(('127.0.0.1', 1002)) # 127.0.0.1
while True:
command = input('>>>')
if command == 'stop':
exit()
command = command.encode('utf-8')
client.send(command)
get = socket.recv(2048)
get = get.decode('utf-8')
print('recieve',get)
server:
import socket
import subprocess
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # AF_INET = IP, SOCK_STREAM = TCP
server.bind(('127.0.0.1', 1002)) # 127.0.0.1
server.listen()
client_socket, client_address = server.accept()
print(type(client_socket))
while True:
get = client_socket.recv(2048)
get = get.decode('utf-8')
print('recieve',get)
output = subprocess.check_output(get, shell=True)
client_socket.send(output)
output from client:
File "C:\Users\rusla\Desktop\client.py", line 15, in <module>
get = socket.recv(1024)
AttributeError: module 'socket' has no attribute 'recv'
I started reading the book "Black hat python" and upon writing the very 1st TCP client example I get the error
AttributeError: 'socket' object attribute 'send' is read-only
The code is as follows:
import socket
target_host = "google.com"
target_port = 80
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client.connect((target_host,target_port))
client.send = ("GET / HTTP/1.1\r\nHost: google.com\r\n\r\n")
response = client.recv(4096)
print response
Where could the mistake be?
Basically I'm just starting out with python networking and python in general and I can't get my TCP client to send data. It says:
Traceback (most recent call last):
File "script.py", line 14, in <module>
client.send(data) #this is where I get the error
TypeError: a bytes-like object is required, not 'str'
The code is as follows:
import socket
target_host = "www.google.com"
target_port = 80
#create socket object
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#connect the client
client.connect((target_host,target_port))
#send some data
data = "GET / HTTP/1.1\r\nHost: google.com\r\n\r\n"
client.send(data) #this is where I get the error
#receive some data
response = client.recv(4096)
print(response)
Thanks for your help in advance!
You are probably using Python 3.X. socket.send() expected a bytes type argument but data is an unicode string. You must encode the string using str.encode() method. Similarly you would use bytes.decode() to receive the data:
import socket
target_host = "www.google.com"
target_port = 80
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((target_host,target_port))
data = "GET / HTTP/1.1\r\nHost: google.com\r\n\r\n"
client.send(data.encode('utf-8'))
response = client.recv(4096).decode('utf-8')
print(response)
If you are using python2.x your code is correct. As in the documentation for python2 socket.send() takes a string parameter. But if you are using python3.x you can see that socket.send() takes a bytes parameter. Thus you have to convert your string data into bytes using str.encode(). So your code might look like this instead.
import socket
target_host = "www.google.com"
target_port = 80
#create socket object
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#connect the client
client.connect((target_host,target_port))
#send some data
data = "GET / HTTP/1.1\r\nHost: google.com\r\n\r\n"
client.send(data.encode('utf-8'))
#receive some data
response = client.recv(4096)
print(response)
So I encoded the data with utf-8 as was suggested by a few people and rewrote my code which fixed the odd syntax error. Now my code works perfectly. Thank you to everyone who posted but especially to #FJSevilla. The working code is as follows:
import socket
target_host = "www.google.com"
target_port = 80
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((target_host,target_port))
data = "GET / HTTP/1.1\r\nHost: google.com\r\n\r\n"
client.send(data.encode('utf-8'))
response = client.recv(4096).decode('utf-8')
print(response)
Another suggestion using Python 3.7 is to add the letter "b" in the message. For example:
s.send(b"GET / HTTP/1.1\r\nHost: google.com\r\n\r\n")
import socket
t_host = "www.google.com"
t_port = 80
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((t_host, t_port))
s.send(b"GET / HTTP/1.1\r\nHost: google.com\r\n\r\n")
response = s.recv(4096)
print(response)
so I have been trying to send packets from a crude tcp client to a crude tcp server using the instruction from a book (I have very little python experience but I do have programming experience). However whenever I do try to send the packets sometimes it works sometimes it hits an error:
Tcp client:
import socket
target_host = "0.0.0.0"
target_port = 9999
# create a socket object
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#connect the client
client.connect((target_host,target_port))
#send some data
client.send("GET / HTTP/1.1\nHost: google.com\r\n\r\n")
#recieve some data
response = client.recv(4096)
print response
Tcp Server:
import socket
import threading
bind_ip = "0.0.0.0"
bind_port = 9999
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((bind_ip,bind_port))
server.listen(5)
print "[*] Listening on %s:%d" % (bind_ip,bind_port)
# this is our client handeling thread
def handle_client(client_socket):
# priint out what client says
request = client_socket.recv(1024)
print "[*] Recieved %s" % request
#send back a packet
client_socket.send("ACK!")
client_socket.close()
while True:
client,addr = server.accept()
print "[*] Accepted connection from %s:%d" % (addr[0],addr[1])
# spin up on our client thread to handle incoming data
client_handler = threading.Thread(target=handle_client,args=(client,))
client_handler.start()
Error:
[GCC 5.3.1 20151205]
Python Type "help", "copyright", "credits" or "license" for more information.
[evaluate TCP client.py]
Traceback (most recent call last):
File "/root/TCP client.py", line 10, in <module>
client.connect((target_host,target_port))
File "/usr/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 111] Connection refused
what do I do?
I am trying to make multiple clients and one server using TCP in python. In my client code on sendall its showing error:
sock.sendall(bytes(data + "\n", "utf-8"))
TypeError: str() takes at most 1 argument (2 given)
Code:
import socket
import sys
HOST, PORT = "localhost", 9999
data = " ".join(sys.argv[1:])
# Create a socket (SOCK_STREAM means a TCP socket)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
# Connect to server and send data
sock.connect((HOST, PORT))
sock.sendall(bytes(data + "\n", "utf-8"))
# Receive data from the server and shut down
received = str(sock.recv(1024), "utf-8")
finally:
sock.close()
print("Sent: {}".format(data))
print("Received: {}".format(received))