套接字错误:Errno 111拒绝连接

时间:2015-10-30 16:25:12

标签: python linux sockets raspberry-pi mqtt

我尝试使用RaspberryPi虚拟机(使用Oracle VM VirtualBox)连接到本地托管的HiveMQ代理。我使用Mqtt.fx以及第二个客户端来查看是否收到了已发布的消息。

我正在使用" iot.eclipse.org"在我尝试使用本地hivemq经纪人之前,它工作正常。所以我不太确定问题是什么。也许我只是忘了什么。

即时使用paho mqtt模块。我的图书馆代码被严重破坏:https://pypi.python.org/pypi/paho-mqtt

我正在使用库/模块,然后在另一个.py文件中引用它。 这是库/模块中的代码:

import paho.mqtt.client as mqtt

client = mqtt.Client()                       

#Called on connection to server/broker
def on_connect(client, userdata, rc):                                               
    print("connected with result code"+str(rc))                                 

#Called when new message published to subscribed topic
def on_message(client, userdata, msg):          
    print("NEW PUBLISH: "+msg.topic+" "+str(msg.payload))

#configure connection to the broker
def setup():                                    
    client.on_connect = on_connect
    client.on_message = on_message

#Subscribe
def subscribe(topic):
    print("subscribing to topic: " +topic)
    client.subscribe(topic)

#Connect to broker
def connect():
    client.connect("127.0.0.1", 1883, 60)

def publish(topic, msg):
    client.publish(topic, msg)

这是我实际运行的代码。只有2个简单发布:

import mqttLib as owly

owly.setup()
owly.connect()
owly.publish("test/library", "test okay :)")

num1 = input("1st number: ")
num2 = input("2nd number: ")
mult = num1 * num2

owly.publish("test/mult", mult)

这是运行后得到的消息:

 Traceback (most recent call last):
  File "mqttTest.py", line 6, in <module>
  owly.connect()
  File "/home/rpi/python/MQTT/mqttLib.py", line 27, in connect
  client.connect("127.0.0.1", 1883, 60)
  File "/usr/local/lib/python2.6/dist-packages/paho/mqtt/client.py", line 612, In connect
  return self.reconnect()
  File "/usr/local/lib/python2.6/dist-packages/paho/mqtt/client.py", line 732, in reconnect
  sock = socket.create_connection((self._host, self._port))
  File "/usr/lib/python2.6/socket.py", line 514, in create_connection
  raise error, msg
  socket.error: [Errno 111] Connection refused

它适用于远程托管代理。而不是在本地混淆我。

我已经剥离了HiveMQ上的大部分插件,并在HiveMQ上留下了消息日志插件,所以我不认为它与身份验证有任何关系。

我认为我使用的虚拟机是ubuntu,如果有帮助的话!

感谢您提前的时间!

已编辑

添加了ifconfig输出:

rpi@RaspberryPi:~$ ifconfig 
eth0      Link encap:Ethernet  HWaddr 08:00:27:5b:26:64  
      inet addr:192.168.56.101  Bcast:192.168.56.255  Mask:255.255.255.0
      inet6 addr: fe80::a00:27ff:fe5b:2664/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:607 errors:0 dropped:0 overruns:0 frame:0
      TX packets:12 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000 
      RX bytes:82290 (82.2 KB)  TX bytes:1920 (1.9 KB)

lo    Link encap:Local Loopback  
      inet addr:127.0.0.1  Mask:255.0.0.0
      inet6 addr: ::1/128 Scope:Host
      UP LOOPBACK RUNNING  MTU:16436  Metric:1
      RX packets:0 errors:0 dropped:0 overruns:0 frame:0
      TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:0 
      RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

添加了netstat -anp输出:

rpi@RaspberryPi:~$ netstat -anp
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
 Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State PID/Program name
udp        0      0 0.0.0.0:68              0.0.0.0:*                               -               
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name    Path
unix  2      [ ACC ]     STREAM     LISTENING     5465     1017/lxterminal     /tmp/.lxterminal-socket:0.0-rpi
unix  2      [ ACC ]     STREAM     LISTENING     2354     -                   @/com/ubuntu/upstart
unix  2      [ ACC ]     STREAM     LISTENING     3653     -                   /tmp/.X11-unix/X0
unix  2      [ ACC ]     STREAM     LISTENING     3652     -                   @/tmp/.X11-unix/X0
unix  2      [ ]         DGRAM                    2462     -                   @/org/kernel/udev/udevd
unix  2      [ ACC ]     STREAM     LISTENING     4693     891/dbus-daemon     @/tmp/dbus-DYueHXFGGf
unix  2      [ ACC ]     STREAM     LISTENING     4524     814/gnome-keyring-d /tmp/keyring-OM3Fh4/control
unix  2      [ ACC ]     STREAM     LISTENING     5019     913/gam_server      @/tmp/fam-rpi-
unix  2      [ ]         DGRAM                    5068     -                   @/org/freedesktop/hal/udev_event
unix  2      [ ACC ]     STREAM     LISTENING     4680     -                   /tmp/ssh-ESoUnvu822/agent.822
unix  2      [ ACC ]     STREAM     LISTENING     5037     -                   @/var/run/hald/dbus-ejQUT9OaXl
unix  2      [ ACC ]     STREAM     LISTENING     4832     904/pcmanfm         /tmp/.pcmanfm-socket:0.0-rpi
unix  2      [ ACC ]     STREAM     LISTENING     3497     -                   /var/run/dbus/system_bus_socket
unix  2      [ ACC ]     STREAM     LISTENING     4834     909/menu-cached     /tmp/.menu-cached-:0-rpi
unix  2      [ ACC ]     STREAM     LISTENING     4966     814/gnome-keyring-d /tmp/keyring-OM3Fh4/pkcs11
unix  6      [ ]         DGRAM                    3504     -                   /dev/log
unix  2      [ ACC ]     STREAM     LISTENING     5014     814/gnome-keyring-d /tmp/keyring-OM3Fh4/ssh
unix  2      [ ACC ]     STREAM     LISTENING     5042     -                   @/var/run/hald/dbus-9woYq1KdE9
unix  3      [ ]         STREAM     CONNECTED     5468     -                   
unix  3      [ ]         STREAM     CONNECTED     5467     1017/lxterminal     
unix  3      [ ]         STREAM     CONNECTED     5463     -                   @/tmp/.X11-unix/X0
unix  3      [ ]         STREAM     CONNECTED     5462     1017/lxterminal     
unix  2      [ ]         DGRAM                    5365     -                   
unix  3      [ ]         STREAM     CONNECTED     5288     -                   @/var/run/hald/dbus-ejQUT9OaXl
unix  3      [ ]         STREAM     CONNECTED     5287     -                   
unix  3      [ ]         STREAM     CONNECTED     5283     -                   @/var/run/hald/dbus-ejQUT9OaXl
unix  3      [ ]         STREAM     CONNECTED     5239     -                   
unix  3      [ ]         STREAM     CONNECTED     5237     -                   /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     5236     -                   
unix  3      [ ]         STREAM     CONNECTED     5144     -                   @/var/run/hald/dbus-ejQUT9OaXl
unix  3      [ ]         STREAM     CONNECTED     5134     -                   
unix  3      [ ]         STREAM     CONNECTED     5063     -                   @/var/run/hald/dbus-9woYq1KdE9
unix  3      [ ]         STREAM     CONNECTED     5062     -                    
unix  3      [ ]         STREAM     CONNECTED     5039     -                   /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     5038     -                   
unix  3      [ ]         STREAM     CONNECTED     5025     -                   /var/run/dbus/system_bus_socket
 unix  3      [ ]         STREAM     CONNECTED     5024     904/pcmanfm         
 unix  3      [ ]         STREAM     CONNECTED     5022     913/gam_server      @/tmp/fam-rpi-
 unix  3      [ ]         STREAM     CONNECTED     5021     904/pcmanfm         
 unix  2      [ ]         DGRAM                    4969     814/gnome-keyring-d 
 unix  3      [ ]         STREAM     CONNECTED     4964     891/dbus-daemon     @/tmp/dbus-DYueHXFGGf
 unix  3      [ ]         STREAM     CONNECTED     4963     814/gnome-keyring-d 
 unix  3      [ ]         STREAM     CONNECTED     4842     909/menu-cached     /tmp/.menu-cached-:0-rpi
 unix  3      [ ]         STREAM     CONNECTED     4838     902/lxpanel         
 unix  3      [ ]         STREAM     CONNECTED     4830     -                   @/tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     4829     904/pcmanfm         
 unix  3      [ ]         STREAM     CONNECTED     4796     -                   @/tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     4795     901/xscreensaver    
 unix  3      [ ]         STREAM     CONNECTED     4788     -                   @/tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     4787     902/lxpanel         
 unix  3      [ ]         STREAM     CONNECTED     4762     -                   @/tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     4761     897/openbox         
 unix  3      [ ]         STREAM     CONNECTED     4701     -                   @/tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     4700     822/lxsession       
 unix  3      [ ]         STREAM     CONNECTED     4697     -                   @/tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     4696     890/dbus-launch     
 unix  3      [ ]         STREAM     CONNECTED     4695     891/dbus-daemon     
 unix  3      [ ]         STREAM     CONNECTED     4694     891/dbus-daemon     
 unix  3      [ ]         STREAM     CONNECTED     4683     -                   @/tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     4682     890/dbus-launch     
 unix  3      [ ]         STREAM     CONNECTED     4673     -                   @/tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     4672     882/VBoxClient      
 unix  3      [ ]         STREAM     CONNECTED     4671     -                   @/tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     4670     882/VBoxClient      
 unix  3      [ ]         STREAM     CONNECTED     4666     -                   @/tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     4665     875/VBoxClient      
 unix  3      [ ]         STREAM     CONNECTED     4664     -                   @/tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     4663     875/VBoxClient      
 unix  3      [ ]         STREAM     CONNECTED     4656     -                   @/tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     4655     866/VBoxClient      
 unix  3      [ ]         STREAM     CONNECTED     4527     -                   /var/run/dbus/system_bus_socket
 unix  3      [ ]         STREAM     CONNECTED     4526     -                   
 unix  3      [ ]         STREAM     CONNECTED     4440     -                   /var/run/dbus/system_bus_socket
 unix  3      [ ]         STREAM     CONNECTED     4439     -                   
 unix  3      [ ]         STREAM     CONNECTED     4415     -                   /var/run/dbus/system_bus_socket
 unix  3      [ ]         STREAM     CONNECTED     4414     -                   
 unix  3      [ ]         STREAM     CONNECTED     4402     -                   /var/run/dbus/system_bus_socket
 unix  3      [ ]         STREAM     CONNECTED     4401     -                   
 unix  2      [ ]         DGRAM                    4400     -                   
 unix  2      [ ]         DGRAM                    4355     -                   
 unix  3      [ ]         STREAM     CONNECTED     4356     -                   @/tmp/.X11-unix/X0
 unix  3      [ ]         STREAM     CONNECTED     3663     -                   
 unix  3      [ ]         STREAM     CONNECTED     3517     -                   /var/run/dbus/system_bus_socket
 unix  3      [ ]         STREAM     CONNECTED     3516     -                   
 unix  3      [ ]         STREAM     CONNECTED     3515     -                   
 unix  3      [ ]         STREAM     CONNECTED     3514     -                   
 unix  3      [ ]         DGRAM                    2494     -                   
 unix  3      [ ]         DGRAM                    2493     -               unix  3      [ ]         STREAM     CONNECTED     2445     -                   @/com/ubuntu/upstart
 unix  3      [ ]         STREAM     CONNECTED     2444     -                   

[R

1 个答案:

答案 0 :(得分:2)

如评论中所述,127.0.0.1是VM的本地地址,您需要使用的是运行代理的主机的IP地址。

在这种情况下,代理主机位于VM主机上,您可以使用VM上的ip route命令获取VM主机的地址,它的输出将如下所示:

default via 192.168.56.100 dev eth0 
192.168.56.0/24 dev eth0  proto kernel  scope link  src 192.168.56.100

您要用于代理的IP地址应该是以默认开头的行中的IP地址,在示例中为192.168.56.100