pythonsocketsifttt

Python socket.error


I found this script on aaronbell.com with which I´m trying to use my Dashbutton to connect to IFTTT. My Pi is throwing this error:

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

and here is my script:

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

I tried changing line 30 to:

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

but a similar error occures:

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

Thanks in advance for your help!


Solution

  • CHANGE THE LINE

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

    TO THIS

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

    This will work.

    First, domain should be set to "AF_INET", just like in the struct sockaddr_in (above.) Next, the type argument tells the kernel what kind of socket this is: SOCK_STREAM or SOCK_DGRAM. Finally, just set protocol to "0" to have socket() choose the correct protocol based on the type. (Notes: there are many more domains than I've listed. There are many more types than I've listed. See the socket() man page. Also, there's a "better" way to get the protocol. See the getprotobyname() man page.)