gpt4 book ai didi

python - <类 'socket.error' >([错误 111] 连接被拒绝)

转载 作者:行者123 更新时间:2023-11-28 19:18:25 24 4
gpt4 key购买 nike

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

Traceback (most recent call last): File "/opt/web-apps/web2py/gluon/restricted.py", line 227, in restricted exec ccode in environment File "/opt/web-apps/web2py/applications/Home_Plugs/controllers/default.py", line 85, in File "/opt/web-apps/web2py/gluon/globals.py", line 393, in self._caller = lambda f: f() File "/opt/web-apps/web2py/gluon/tools.py", line 3440, in f return action(*a, **b) File "/opt/web-apps/web2py/applications/Home_Plugs/controllers/default.py", line 32, in toggle GPIO.setup(light.OnPin,GPIO.OUTPUT) File "applications/Home_Plugs/modules/GPIOClient.py", line 23, in setup File "applications/Home_Plugs/modules/GPIOClient.py", line 18, in send host = '192.168.1.79' File "/usr/lib64/python2.7/socket.py", line 224, in meth return getattr(self._sock,name)(*args) error: [Errno 111] Connection refused

服务器代码:

#!/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)

任何帮助将不胜感激

需要更多信息请告诉我

提前致谢

最佳答案

原来在我的 GPIOClient.py 中有 2 个错误的回车符

删除,现在一切正常

关于python - <类 'socket.error' >([错误 111] 连接被拒绝),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30548380/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com