Python 中的 Raspberry PI 服务器/客户端套接字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22737838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:31:49  来源:igfitidea点击:

Raspberry PI Server/Client Socket in Python

pythonsocketsraspberry-piosx-mavericksraspbian

提问by user3476808

I am trying to set up a Python socket between my Raspberry Pi (running Raspbian) and my Macbook Pro (running Mavericks).

我正在尝试在我的 Raspberry Pi(运行 Raspbian)和我的 Macbook Pro(运行 Mavericks)之间设置一个 Python 套接字。

Both devices are connected to the same WiFi network in my appt. I run the server code on my RPi and then the client code on my Macbook (I have also tried the reverse). I think that I am missing a set up step because the code I am using I found on multiple sites, so I assume it works. I also checked that my Macbook has firewall turned off.

两台设备都连接到我的应用程序中的同一个 WiFi 网络。我在我的 RPi 上运行服务器代码,然后在我的 Macbook 上运行客户端代码(我也试过相反的)。我认为我缺少一个设置步骤,因为我在多个站点上找到了我正在使用的代码,所以我认为它可以工作。我还检查了我的 Macbook 是否关闭了防火墙。

Server Code:

服务器代码:

from socket import *

host = "127.0.0.1"

print host

port = 7777

s = socket(AF_INET, SOCK_STREAM)

print "Socket Made"

s.bind((host,port))

print "Socket Bound"

s.listen(5)

print "Listening for connections..."

q,addr = s.accept()

data = raw_input("Enter data to be sent: ")

q.send(data)  

Client Code:

客户代码:

from socket import *

host = "127.0.0.1"

print host

port=4446

s=socket(AF_INET, SOCK_STREAM)

print "socket made"

s.connect((host,port))

print "socket connected!!!"

msg=s.recv(1024)

print "Message from server : " + msg

I get the error:

我收到错误:

Traceback (most recent call last):
  File "TCPclient.py", line 9, in <module>
    s.connect((host,port))         
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py",

line 224, in meth return getattr(self._sock,name)(*args) socket.error: [Errno 61] Connection refused

Traceback (most recent call last):
  File "TCPclient.py", line 9, in <module>
    s.connect((host,port))         
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py",

第 224 行,在 meth 中返回 getattr(self._sock,name)(*args) socket.error: [Errno 61] 连接被拒绝

My process for executing the code is:

我执行代码的过程是:

  • type "python TCPserver.py" into RPi terminal

  • type "python TCPclient.py into Macbook terminal

  • 在 RPi 终端中输入“python TCPserver.py”

  • 在 Macbook 终端中输入“python TCPclient.py”

Then I receive the error message on my Macbook, no error on the RPi

然后我在 Macbook 上收到错误消息,在 RPi 上没有错误

My questions are:

我的问题是:

  1. Is 127.0.0.1 the proper input for "host"? (please note I also tried "localhost")
  2. Does the input for host have to be the same for the client and server code?
  3. Should the RPi and Macbook both be connected to the same WiFi network?
  4. Is there any set up that needs to be done on either the RPi or my Macbook in order for this to work (Please note my RPi is Model B, new, and I did not set up anything else on it before this)
  5. Do you know why I am receiving this error and how to fix it?
  1. 127.0.0.1 是“主机”的正确输入吗?(请注意我也试过“本地主机”)
  2. 客户端和服务器代码的主机输入是否必须相同?
  3. RPi 和 Macbook 应该连接到同一个 WiFi 网络吗?
  4. 是否需要在 RPi 或我的 Macbook 上进行任何设置才能使其正常工作(请注意,我的 RPi 是 Model B,新的,在此之前我没有在上面设置任何其他内容)
  5. 你知道为什么我会收到这个错误以及如何解决它吗?

Your help is greatly appreciated!!

非常感谢您的帮助!!

回答by Nicolas Defranoux

127.0.0.1 is a special IP address for local machine.
You must set the real IP address (on your LAN) of you mac in the client code.
You should also bind on this IP on the server, or on 0.0.0.0 to bind on all available IP addresses.
You must also use the same port number on both client and server.

And to reply to your questions:

127.0.0.1 是本地机器的特殊 IP 地址。
您必须在客户端代码中设置您 mac 的真实 IP 地址(在您的 LAN 上)。
您还应该绑定到服务器上的这个 IP,或者绑定到 0.0.0.0 以绑定所有可用的 IP 地址。
您还必须在客户端和服务器上使用相同的端口号。

并回答您的问题:

Is 127.0.0.1 the proper input for "host"? (please note I also tried "localhost")
127.0.0.1 is the same than localhost, it means the local machine. This will work if you run the client and the server on the same machine, else you need the real IP address of your mac. Try ifconfigin a console.

Does the input for host have to be the same for the client and server code?
Yes and no. On the server you bindto a port and an address, so you'll wait for connections on this port and address. You can use the IP address, or 0.0.0.0.

Should the RPi and Macbook both be connected to the same WiFi network?
Yes and no. It will work with the same WiFi network, but it will also work with two different WiFi networks if they are connected together directly or with a IP router. Most of the time though they are connected to the internet through a NAT (network address translator), and then it will not work.

Is there any set up that needs to be done on either the RPi or my Macbook in order for this to work (Please note my RPi is Model B, new, and I did not set up anything else on it before this)
I don't know much about the RPi but it looks like standard TCP sockets, that should work out of the box.

Do you know why I am receiving this error and how to fix it?
As I stated at the beginning: You try to connect to the RPi (127.0.0.1) on the wrong port.

127.0.0.1 是“主机”的正确输入吗?(请注意,我也试过“localhost”)
127.0.0.1 与 localhost 相同,表示本地机器。如果您在同一台机器上运行客户端和服务器,这将起作用,否则您需要 mac 的真实 IP 地址。在控制台中尝试ifconfig

客户端和服务器代码的主机输入是否必须相同?
是和否。在服务器上,您绑定到一个端口和一个地址,因此您将等待此端口和地址上的连接。您可以使用 IP 地址或 0.0.0.0。

RPi 和 Macbook 应该连接到同一个 WiFi 网络吗?
是和否。它适用于同一个 WiFi 网络,但如果它们直接连接在一起或通过 IP 路由器连接在一起,它也适用于两个不同的 WiFi 网络。大多数情况下,尽管它们通过 NAT(网络地址转换器)连接到 Internet,但随后将无法正常工作。

是否需要在 RPi 或我的 Macbook 上进行任何设置才能使其正常工作(请注意,我的 RPi 是 Model B,新的,在此之前我没有在上面设置任何其他东西)
我不'对 RPi 了解不多,但它看起来像标准的 TCP 套接字,应该是开箱即用的。

你知道为什么我会收到这个错误以及如何解决它吗?
正如我在开头所说:您尝试在错误的端口上连接到 RPi (127.0.0.1)。

回答by Ahmed S. El-Afifi

You have created a listener on port 7777, then you connected on 4446 !!!!!

您已经在端口 7777 上创建了一个侦听器,然后您在 4446 上连接了!!!!!

just connect on the same port you are listening to =)

只需连接到您正在侦听的同一端口 =)