I am trying to transfer scapy packets between two python apps that I wrote. My first python code is sending packet using scapy to the localhost on port 9000. My second python code is listening on the localhost using sockets. But for some reason I can't get the packet on the second code although its shows me that the packet was sent.
Can someone tell me how can I achived this goal?
This is my 'Server':
import socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.bind(("127.0.0.1", 9000))
sock.listen(1)
csock, addr = sock.accept()
data = csock.recv(2048)
print(data)
This is my 'Client' code:
import scapy.all as scapy
def main():
packet = scapy.IP(dst="127.0.0.1") / scapy.TCP(dport=9000) / scapy.Raw(load="data")
scapy.send(packet)
if __name__ == "__main__":
main()
The output is that the client shows that the packet sent and stop running.
And the server stays on the csock, addr = sock.accept
Related
I have an Ipad and a raspberry pi. I want to broadcast a simple message from my ipad to my raspberry pi using python's library: "socket". I have a file called server.py in my raspberry pi. I have another file called client.py in my Ipad. server.py should await a connection from the Ipad, and accept it. client.py should send the broadcast message.
server.py
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 {}:{}".format(bind_ip, bind_port))
def handle_client(client_socket):
request = client_socket.recv(1024)
print('received: {}'.format(request))
client_socket.send(b'ACK!')
client_socket.close()
while True:
client, addr = server.accept()
print("[*] Accepted connection from: {}:{}".format(addr[0], addr[1]))
client_handler = threading.Thread(target=handle_client, args=(client,))
client_handler.start()
client.py
import socket
HOST = "0.0.0.0"
PORT = 9999
sock = socket.socket()
print("Attempting connection... ")
sock.connect((HOST, PORT))
print("Connected")
I first ran server.py on my raspberry pi, then ran client.py on my Ipad. However, the following error message greeted me when I ran client.py:
[Errno 61] Connection refused
I made sure the server was running properly, and I checked where the client was connecting to. It should have worked.
Please help me.
I would like to point out that there are so many mistakes in that code. I presume that you are totally new with sockets.
(Also try putting 'your-server's-local-ip' in the bind_ip and HOST in server.py and client.py respectively if both of your server and client are connected to the same network)
Even though you connect with the server you'd still get greeted with another errors.
Let me start with server.py:
In server.py you are using handle_client function to recieving a message from the client while at the client end you are not sending anything.
After that you're recieving the message from the client while the client is not sending anything.
There is no broadcast function for sending the message to every connected client
The main issue is that you've not learned the basics of the sockets, my only suggestion to you would be to, start things small and then increase the complexity but here, that case is totally opposite.
You can also refer to this video to learn the basics : https://youtu.be/u4kr7EFxAKk
I'm trying to make a console chat app in python using socket library.
Whenever I send a message to the server, the server code crashes with the following message:
OSError: [WinError 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied
Server code
import socket
HOST = socket.gethostbyname(socket.gethostname()) # get the ip address of PC
PORT = 5050
ADDRESS = (HOST, PORT)
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.bind(ADDRESS)
while True:
socket.listen()
conn, addr = socket.accept()
print(f"Connected by {addr}")
while True:
data = conn.recv(64)
print(data.decode('utf-8'))
socket.send(data)
Client code
import socket
HOST = socket.gethostbyname(socket.gethostname()) # get the ip address of PC
PORT = 5050
ADDRESS = (HOST, PORT)
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.connect(ADDRESS)
while True:
msg = input("Enter your message")
socket.send(msg.encode('utf-8'))
data = socket.recv(64)
print(data.decode('utf-8'))
What I am trying to achieve is whenever I send a message to the server, the client script should print the sent message. How can I fix that?
You're attempting to send data to your own server socket. You instead want to send to the client that you accepted.
socket.send(data)
Should be:
conn.send(data)
If you think about it, if you had multiple clients, how would you send data to a specific client? By using the socket that accept gave you.
As a side note, you probably don't want to import the module as socket, and also call your variable socket. It's fine here, but if you were to make a more complicated project, you may accidentally refer to the object when you meant to refer to the module. I'd rename the socket object to sock or server_socket to avoid shadowing.
I have a simple socket written in python to send a request to the server(Virtualbox Kali Linux) and receive some data from it. It seems like, it is connecting, but receiving no data. Screenshot of behaviour - https://prnt.sc/ragj7q
Here you can see a network config of the Virtualbox
Sever code:
import socket
def SocketMon():
serversocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
serversocket.bind((socket.gethostname(),1234))
serversocket.listen(5)
while True:
clientsocket,address = serversocket.accept()
clientsocket.send(bytes("welcome"),"utf-8")
SocketMon()
Client code:
import socket
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(("127.0.0.1",1234))
msg = s.recv(1024)
print(msg.decode("utf-8"))
I am trying to establish UDP conection between 2 of my computers (1 and 2).
I succeeded in establish UDP connection in which computer 1 is the client ("sending") and computer 2 is the server ("reciving").
Now I am trying to do the opposite: I want computer 2 to be the client and computer 1 to be the server. I am using the same programs as I have written before, however it doesn't work.
When I use Wireshark, I can see the sended messeges from computer 2, however the python doesn't recive them...
Any Ideas?
here is my code:
client:
import socket
IP_robot = '127.0.0.1'
port_robot = 8830
def send_data(V,IP,port):
my_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
my_socket.connect((IP,port))
MESSAGE=str(V)
my_socket.send(MESSAGE)
my_socket.close
V="123456"
send_data(V,IP_robot,port_robot)
where in my program IP_robot is the IP of the server.
server:
import socket
IP_commands = "0.0.0.0"
port_commands = 8830
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((IP_commands, port_commands))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print data
edit:
I have tried to use TCP and it still dosent work. It seemes that one of my computers refuses to recive it's messges in python... any sugesstions?
I've tried to connect two computers with a socket in Python and I don't know why it doesn't work. The files are from internet and it compiles for me but without any results.
The server.py:
#!/usr/bin/python
import socket
s = socket.socket()
host = ''
port = 12345
s.bind((host, port))
s.listen(5)
while True:
c, addr = s.accept()
print 'Got connection from', addr
c.send('Thank you for connecting')
c.close()
and the client.py:
#!/usr/bin/python
import socket
s = socket.socket()
host = # here I put the ip of the server's laptop
port = 12345
s.connect((host, port))
print s.recv(1024)
s.close()
What's wrong?
You have to run the server first. Then run the client at the same time with the IP of the server (I used localhost because it was running on one computer, maybe you should try if that works). The code worked fine for me, every time I ran the client, the server printed a message. If it doesn't work for you, maybe your firewall is not letting you open ports.
Just for the future, please always post any error messages you see.
BTW, isn't this the Python Documentation example for sockets?