I am trying to create a small program that sends a Hello World to my laptop from my Raspberry Pi, I am on the same network however, the code below seems not to work. I feel like it might be a firewall issue.
Raspberry Pi's code
import socket
ip = "127.0.0.1"
port = 5005
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((ip, port))
message = b"Hello World"
sock.sendto(message, (ip, port))
Laptop's code
import socket
ip = "127.0.0.1"
port = 5005
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((ip, port))
while True:
data, addr = sock.recvfrom(1024)
print("received message: %s" % data)
There are no error messages, just no data is displayed.
Should be noted that when both of these files are ran on the same machine they work however, when on two different machines they do not.
Related
I want to send a simple UDP message from my local computer(client.py) to a remote server pythonanywhere(server.py). I don't actually know if I'm doing it right, or maybe what I did is not a good practice. How can I do that? I'm still a beginner in socket programming.
client.py(local computer)
import socket
ip = "<insert ip here>"
port = 9999
Message = "Hello, Server"
clientSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
clientSock.sendto(Message.encode('utf-8'), (ip, port))
server.py(pythonanywhere)
import socket
ip = "<insert ip here>"
port = 9999
serverSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
serverSock.bind((ip, port))
while True:
data, addr = serverSock.recvfrom(4096)
print("Message: ", data)
You cannot run a udp socket server on PythonAnywhere. PythonAnywhere does not route arbitrary network traffic to the machines where you would be running the server code.
I have a simple server-client combo running on 2 computers in 2 different networks. The server (a Raspberry) has a TCP tunnel running.
Now running on the code on localhost is fine, on 2 different machines with correct IP and port (I can ping the server via telnet and establish connection via ssh using the same IP and port), I just get the following on the client side AND NOTHING ELSE:
Received SSH-2.0-OpenSSH_7.9p1 Raspbian-10+deb10u2
I should instead receive: Received b'Hello, world'
On the serverside I receive no message at all.
What do I need to run it in 2 different networks? Below the source codes.
server.py:
#!/usr/bin/env python3
import socket
HOST = '' # Standard loopback interface address (localhost)
PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print("Connected by", addr)
while True:
data = conn.recv(1024)
if not data:
break
conn.sendall(data)
client.py:
#!/usr/bin/env python3
import socket
HOST = 'xxx.xxx.xxx.xxx' # The server's hostname or GLOBAL IP address
PORT = 12345 # The port used by the server, not actually 12345
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.sendall(b"Hello, world")
data = s.recv(1024)
print("Received", repr(data))
I know this was already asked but the previous questions didn't help. I'm trying to send some data using sockets. Specifically I'm using my laptop as server and a Linux emulator (Termux) on my smartphone as a client. Here below you can see the two Python codes. For the server:
import socket
HOST = ''
PORT = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
print('Connected by', addr)
while True:
data = conn.recv(1024)
if not data:
break
conn.sendall(data)
s.close()
And for the client:
import socket
HOST = ''
PORT = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall(b'Hello, world')
data = s.recv(1024)
print('Received', repr(data))
s.close()
When I'm connected to the same WiFi and in HOST (in both codes) I put the IP I see from ipconfig (192.168.---.---) everything works. It also works if in the HOST of the server I put 0.0.0.0.
However, when I put the IP of the machine (that I can see on https://whatismyipaddress.com/) and instead of using the WiFi I use the phone connection I get: ConnectionRefusedError: [Errno 111] Connection Refused.
Can someone explain me how can I connect client and server when the networks are different? I have been stuck with this for a while.
I also tried to open a port on the Firewall following this procedure and put it in the code instead of 5555 but still it didn't work.
Thank you in advance for the help.
I have got a server.py and a client.py.
If i run them on the same computer using the same port and host as 127.0.0.1 it works fine.
I got another laptop connected to the same network. I got my local ip address 129.94.209.9 and used that for the server. On the other laptop I tried connecting to the server but I couldn't.
Is this an issue with my code, with the network or I'm just using the wrong ip address?
Server.py
HOST = '129.94.209.9'
PORT = 9999
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((HOST, PORT))
server_socket.listen(10)
sockfd, addr = server_socket.accept()
send and receive messages etc....
Client.py
HOST = '129.94.209.9'
PORT = 9999
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect((HOST, PORT))
except:
print("Cannot connect to the server")
send and receive messages etc...
Client prints "Cannot connect to the server"
Thanks!!
UPDATES: thanks for the comments
1) I did do sockfd, addr = server_socket.accept() sorry I forgot to add it, it was a few lines further down in the code.
2) The error is: [Errno 11] Resource temporarily unavailable
3) Ping does work
EDIT: It is working now when I plug both computers in by ethernet cable to the same network. Not sure why my the won't work when they are right next to each other connected to wifi.
Thanks for all the suggestions! I'll investigate the network issue myself
Using the following code (my IP address rather than yours, of course) I see the expected behaviour.
so8srv.py:
import socket
HOST = '192.168.33.64'
PORT = 9999
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((HOST, PORT))
server_socket.listen(10)
print("Listening")
s = server_socket.accept()
print("Connected")
so8cli.py:
import socket
HOST = '192.168.33.64'
PORT = 9999
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect((HOST, PORT))
except Exception as e:
print("Cannot connect to the server:", e)
print("Connected")
When I run it, the server prints "Listening". When I then run the client, both it and the server print "Connected".
You will notice that, unlike your code, my client doesn't just print hte same diagnostic for any exception that's raised, but instead reports the exception. As a matter of sound practice you should avoid bare except clauses, since your program will then take the same action whether the exception is caused by a programming error or a user action such as KeyboardInterrupt.
Also try using this construction to obtain the IP address:
HOST=socket.gethostbyname(socket.gethostname())
I'm using the Python socket library to send a message to another server and measure the roundtrip latency. I'm new networks so it's very basic, just using the send-receive examples given in the socket docs (http://wiki.python.org/moin/UdpCommunication). For the moment I'm using time.time() to timestamp when the message goes out and when it comes back, though I want to be able to get nanosecond accuracy in the end. Right now, I get about 300microseconds through my python script but pinging from the shell yields about 100micros. What can I do to get faster communication and/or more accurate measurement?
Script that sends one message:
import socket
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
MESSAGE = "Hello, World!"
print "UDP target IP:", UDP_IP
print "UDP target port:", UDP_PORT
print "message:", MESSAGE
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
print "SENT {0}".format(int(time.time()*1000*1000)
This is the script that listens for the ping to come back
import socket
UDP_IP = "originating side localhost ip"
UDP_PORT = 9112
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
if data is not None:
print "received message: {0} {1}".format(data, int(time.time()*1000*1000)
This is the script that listens on the server being pinged. When it receives a message on the port, it sends a message right back
import socket
UDP_IP = "pinged server's localhost ip"
UDP_PORT = 9111
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
UDP_IP2 = "ip of server that sent ping"
UDP_PORT2 = 9112
MESSAGE = "HEY"
sock2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
print "Listening on PORT 9111"
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
if data is not None:
sock.sendto(MESSAGE, (UDP_IP2, UDP_PORT2))
print "received message: ", data