<class'socket.error'=“”>([Errno 111]拒绝连接)

时间:2015-05-30 16:12:27

标签: python sockets python-2.7 connection gpio

我正在使用python开发一个家庭自动化应用程序,但自从我的本地设置迁移到两台物理机(服务器,客户端)后,我收到连接拒绝错误:

  

Traceback(最近一次调用最后一次):文件   “/opt/web-apps/web2py/gluon/restricted.py”,第227行,受限制   exec ccode在环境文件中   “/opt/web-apps/web2py/applications/Home_Plugs/controllers/default.py”   第85行,在文件“/opt/web-apps/web2py/gluon/globals.py”中,   第393行,在self._caller = lambda f:f()文件中   “/opt/web-apps/web2py/gluon/tools.py”,第3440行,返回   动作(* a,** b)文件   “/opt/web-apps/web2py/applications/Home_Plugs/controllers/default.py”   第32行,切换GPIO.setup(light.OnPin,GPIO.OUTPUT)文件   设置中的“applications / Home_Plugs / modules / GPIOClient.py”,第23行   文件“applications / Home_Plugs / modules / GPIOClient.py”,第18行,发送       host ='192.168.1.79'文件“/usr/lib64/python2.7/socket.py”,第224行,在meth       return getattr(self._sock,name)(* args)错误:[Errno 111]拒绝连接

服务器代码

#!/usr/bin/env python
import socket
import RPi.GPIO as GPIO
import sys
import logging

SETUP = chr(0)
OUTPUT = chr(1)

GPIO.setmode(GPIO.BOARD)

def gpio_setup(data):
    pin,dir = ord(data[0]),ord(data[1])
    GPIO.setup(pin,dir)
    logging.gpioServerLog("setup" + str(pin) + str(dir))
    return 0

def gpio_output(data):
    pin,val = ord(data[0]),ord(data[1])
    GPIO.output(pin,val)
    logging.gpioServerLog("out" + str(pin) + str(val))
    return 0

if __name__=='__main__':
    HOST = ''
    PORT = 21567
    BUFSIZ = 1024
    ADDR = (HOST, PORT)
    serversock = socket.socket()
    serversock.bind(ADDR)
    serversock.listen(5)

    while 1:
        ret = None
        logging.gpioServerLog('waiting for connection...')
        clientsock, addr = serversock.accept()
        logging.gpioServerLog('...connected from:' + str(addr))
        data = clientsock.recv(BUFSIZ)
        if data[0] == SETUP:
            ret = gpio_setup(data[1:])
        elif data[0] == OUTPUT:
            ret = gpio_output(data[1:])

        if  ret:
        clientsock.send('Thank you for conencting')
            clientsock.send(ret)
            clientsock.close()

客户代码

#!/usr/bin/env python
# coding: utf8

import socket
import struct

SETUP_CMD = chr(0)
OUTPUT_CMD = chr(1)

OUTPUT = chr(0)
INPUT = chr(1)

ON = chr(1)
OFF = chr(0)

def send(data):
     sock = socket.socket()
     host = '192.168.1.79'
     port = 21567
     sock.connect((host,port))
     sock.send(data)
     sock.close

def setup(pin,dir):
    data = struct.pack("ccc",SETUP_CMD,chr(pin),dir)
    send(data)

def output(pin,val):
    data = struct.pack("ccc",OUTPUT_CMD,chr(pin),val)
    send(data)

机器: 服务器 - Raspbian Wheezy / Python 2.7 客户端 - CentOS 7 / Python 2.7

我已禁用所有防火墙以确保不会阻止连接。 Telnet工作

我还有一个测试client.py脚本,当从客户端命令行运行时也可以运行:

#!/usr/bin/python
# This is client.py file

import socket               # Import socket module
import GPIOClient as GPIO
import time

GPIO.setup(11,GPIO.OUTPUT)
GPIO.output(11,GPIO.ON)
time.sleep(1)
GPIO.setup(11,GPIO.INPUT)

非常感谢任何帮助

更多信息需要请告诉我

先谢谢

1 个答案:

答案 0 :(得分:0)

原来我的GPIOClient.py

中有2个不正确的回车

删除,现在一切正常