英文:
Problem with socket connect between raspberry (client) and host
问题
I've a Raspberry Pi 3 Model B. I would like to send data from raspberry to host. I use python program with socket package.
I've problem when my raspberry is client and my laptop is server.
I got next error:
OSError: [Errno 113] No route to host
Code client.py:
import socket
HOST = '192.168.0.107'
PORT = 5353
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s send(b'Hello, world')
data = s.recv(1024)
print('Received', repr(data))
server.py:
hostname = socket.gethostname()
HOST = socket.gethostbyname(hostname)
print(HOST)
PORT = 5353
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(('', PORT))
s.listen(1)
conn, addr = s.accept()
with conn:
print('Connected by', addr)
print(conn)
while True:
data = conn.recv(1024)
print(data)
if not data: break
conn.sendall(data)
ifconfig on server:
enp4s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.0.107 netmask 255.255.255.0 broadcast 192.168.0.255
inet6 fe80::ab65:70bf:9921:1d4b prefixlen 64 scopeid 0x20<link>
ether 18:31:bf:51:9d:9c txqueuelen 1000 (Ethernet)
RX packets 213759 bytes 177479962 (169.2 MiB)
RX errors 0 dropped 27 overruns 0 frame 0
TX packets 144335 bytes 25485658 (24.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 43423 bytes 3729254 (3.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 43423 bytes 3729254 (3.5 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
When I swap them i.e. raspberry becomes a server then all messages are send
raspberry: sudo ufw status
Status: active
To Action From
-- ------ ----
SSH ALLOW Anywhere
OpenSSH ALLOW Anywhere
80 ALLOW Anywhere
443 ALLOW Anywhere
443/tcp ALLOW Anywhere
5353 ALLOW Anywhere
5353/tcp ALLOW Anywhere
SSH (v6) ALLOW Anywhere (v6)
OpenSSH (v6) ALLOW Anywhere (v6)
80 (v6) ALLOW Anywhere (v6)
443 (v6) ALLOW Anywhere (v6)
443/tcp (v6) ALLOW Anywhere (v6)
5353 (v6) ALLOW Anywhere (v6)
5353/tcp (v6) ALLOW Anywhere (v6)
raspberry: netstat -lntu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:53 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 :::53 :::* LISTEN
tcp6 0 0 ::1:631 :::* LISTEN
udp 0 0 0.0.0.0:33841 0.0.0.0:*
udp 0 0 0.0.0.0:53 0.0.0.0:*
udp 0 0 0.0.0.0:68 0.0.0.0:*
udp 0 0 0.0.0.0:631 0.0.0.0:*
udp 0 0 0.0.0.0:5353 0.0.0.0:*
udp6 0 0 :::48624 :::*
udp6 0 0 :::53 :::*
udp6 0 0 :::5353 :::*
I think my problem is that port 5353 is not open on tcp. But the command
sudo ufw allow 5353/tcp
does not help.
Also, I reset my ufw's rules:
sudo ufw reset
added new rules like
sudo ufw allow SSH
sudo ufw allow OpenSSH
sudo ufw allow 80
sudo ufw allow 443
sudo ufw allow 5353/tcp
and I disabled and enabled ufw.
It's not worked.
英文:
I've a Raspberry Pi 3 Model B. I would like to send data from raspberry to host. I use python program with socket package.
I've problem when my raspberry is client and my laptop is server.
I got next error:
OSError: [Errno 113] No route to host
Code client.py:
import socket
HOST = '192.168.0.107'
PORT = 5353
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.send(b'Hello, world')
data = s.recv(1024)
print('Received', repr(data))
server.py:
hostname = socket.gethostname()
HOST = socket.gethostbyname(hostname)
print(HOST)
PORT = 5353
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(('', PORT))
s.listen(1)
conn, addr = s.accept()
with conn:
print('Connected by', addr)
print(conn)
while True:
data = conn.recv(1024)
print(data)
if not data: break
conn.sendall(data)
ifconfig on server:
enp4s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.0.107 netmask 255.255.255.0 broadcast 192.168.0.255
inet6 fe80::ab65:70bf:9921:1d4b prefixlen 64 scopeid 0x20<link>
ether 18:31:bf:51:9d:9c txqueuelen 1000 (Ethernet)
RX packets 213759 bytes 177479962 (169.2 MiB)
RX errors 0 dropped 27 overruns 0 frame 0
TX packets 144335 bytes 25485658 (24.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 43423 bytes 3729254 (3.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 43423 bytes 3729254 (3.5 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
When I swap them i.e. raspberry becomes a server then all messages are send
raspberry: sudo ufw status
Status: active
To Action From
-- ------ ----
SSH ALLOW Anywhere
OpenSSH ALLOW Anywhere
80 ALLOW Anywhere
443 ALLOW Anywhere
443/tcp ALLOW Anywhere
5353 ALLOW Anywhere
5353/tcp ALLOW Anywhere
SSH (v6) ALLOW Anywhere (v6)
OpenSSH (v6) ALLOW Anywhere (v6)
80 (v6) ALLOW Anywhere (v6)
443 (v6) ALLOW Anywhere (v6)
443/tcp (v6) ALLOW Anywhere (v6)
5353 (v6) ALLOW Anywhere (v6)
5353/tcp (v6) ALLOW Anywhere (v6)
raspberry: netstat -lntu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:53 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 :::53 :::* LISTEN
tcp6 0 0 ::1:631 :::* LISTEN
udp 0 0 0.0.0.0:33841 0.0.0.0:*
udp 0 0 0.0.0.0:53 0.0.0.0:*
udp 0 0 0.0.0.0:68 0.0.0.0:*
udp 0 0 0.0.0.0:631 0.0.0.0:*
udp 0 0 0.0.0.0:5353 0.0.0.0:*
udp6 0 0 :::48624 :::*
udp6 0 0 :::53 :::*
udp6 0 0 :::5353 :::*
I think my problem is that port 5353 is not open on tcp. But the command
sudo ufw allow 5353/tcp
does not help.
Also, I reset my ufw's rules:
sudo ufw reset
added new rules like
sudo ufw allow SSH
sudo ufw allow OpenSSH
sudo ufw allow 80
sudo ufw allow 443
sudo ufw allow 5353/tcp
and I disabled and enabled ufw.
It's not worked.
答案1
得分: 0
已解决:
我的主机是 Fedora。Fedora 有自己的防火墙:链接
我在主机上的操作:
sudo firewall-cmd --state
>> 运行中
firewall-cmd --list-ports
>> [空]
sudo firewall-cmd --add-port=5353/tcp --timeout 15m
>> 成功
firewall-cmd --list-ports
>> 5353/tcp
然后我在树莓派上启动了客户端并获取了数据。耶!
英文:
SOLVED:
My host machine is Fedora. Fedora has its own firewall: link
My action in host:
sudo firewall-cmd --state
>> running
firewall-cmd --list-ports
>> [empty]
sudo firewall-cmd --add-port=5353/tcp --timeout 15m
>> success
firewall-cmd --list-ports
>> 5353/tcp
Then I launched the client on raspberry and got the data. Yippee!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论