首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python socket.error

问Python socket.error
EN

Stack Overflow用户
提问于 2017-07-10 10:51:23
回答 1查看 1.5K关注 0票数 0

我在aaronbell.com上找到了这个脚本,我试图用它来连接到IFTTT。我的Pi正在抛出这个错误:

代码语言:javascript
复制
Traceback (most recent call last):
  File "dash.py", line 30, in <module>
    rawSocket = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(0x0003))
  File "/usr/lib/python2.7/socket.py", line 187, in __init__
    _sock = _realsocket(family, type, proto)
socket.error: [Errno 1] Operation not permitted

这是我的剧本:

代码语言:javascript
复制
import socket
import struct
import binascii
import time
import json
import urllib2

ifttt_key = 'loremipsum'
ifttt_url_button = 'https://maker.ifttt.com/trigger/button_was_pressed/with/key/' + ifttt_key

macs = {
    'AC63BEBA94E1' : 'MiXT4Pi'
}

def trigger_url(url):
    data = '{ "value1" : "' + time.strftime("%Y-%m-%d") + '", "value2" : "' + time.strftime("%H:%M") + '" }'
    req = urllib2.Request(url, data, {'Content-Type': 'application/json'})
    f = urllib2.urlopen(req)
    response = f.read()
    f.close()
    return response

def button_was_pressed():
    print 'triggering button event, response: ' + trigger_url(ifttt_url_button)

rawSocket = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(0x0003))

while True:
    packet = rawSocket.recvfrom(2048)
    ethernet_header = packet[0][0:14]
    ethernet_detailed = struct.unpack("!6s6s2s", ethernet_header)
    # skip non-ARP packets
    ethertype = ethernet_detailed[2]
    if ethertype != '\x08\x06':
        continue
    # read out data
    arp_header = packet[0][14:42]
    arp_detailed = struct.unpack("2s2s1s1s2s6s4s6s4s", arp_header)
    source_mac = binascii.hexlify(arp_detailed[5])
    source_ip = socket.inet_ntoa(arp_detailed[6])
    dest_ip = socket.inet_ntoa(arp_detailed[8])
    if source_mac in macs:
        #print "ARP from " + macs[source_mac] + " with IP " + source_ip
        if macs[source_mac] == 'MiXT4Pi':
            button_was_pressed()
    else:
        print "Unknown MAC " + source_mac + " from IP " + source_ip

我试着把第30行改为:

代码语言:javascript
复制
rawSocket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.htons(0x0003))

但也发生了类似的错误:

代码语言:javascript
复制
Traceback (most recent call last):
  File "dash.py", line 30, in <module>
    rawSocket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.htons(0x0003))
  File "/usr/lib/python2.7/socket.py", line 187, in __init__
    _sock = _realsocket(family, type, proto)
socket.error: [Errno 22] Invalid argument

提前感谢您的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-24 20:07:56

更改行

代码语言:javascript
复制
rawSocket = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(0x0003))

到此

代码语言:javascript
复制
rawSocket = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, 0)

这会管用的。

首先,域应该设置为"AF_INET",就像在struct sockaddr_in中一样(上面)。接下来,类型参数告诉内核这是什么类型的套接字: SOCK_STREAM还是SOCK_DGRAM。最后,只需将协议设置为"0“,让socket()根据类型选择正确的协议。(注:比我列出的域名要多得多。)有更多的类型比我已经列出。请参阅套接字()手册页。另外,有一个“更好”的方式来获得协议。参见getprotobyname()手册页。)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45010140

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档