giampaolo / pyftpdlib

Extremely fast and scalable Python FTP server library
MIT License
1.65k stars 266 forks source link

collision with raspberry pi GPIO callback facility #469

Open littlegreenbean33 opened 6 years ago

littlegreenbean33 commented 6 years ago

Hi,

I am making a script that has an ftp server to receive pictures from a web cam and as well detecs PIR sensor activity in a raspberry pi.

Both work fantastic in separate scripts, if I combine them, the GPIO callbacks stop working.

I am assuming that there is some kind of race condition between the loop of GPIO check and the select in the socket for FTP reading. I just can't figure out. Not skilled enough. If anybody could look into the issue it would be welcome for a large community.

here is a none working stub ` #!/usr/bin/env python3

from http.server import BaseHTTPRequestHandler
from http.server import HTTPServer
from urllib import parse
from urllib.parse import urlencode
import ssl

from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer

import threading
import datetime
import time
import sys
import logging
import json
import signal

from gpiozero import MotionSensor
from gpiozero import LED

serverip='192.168.33.220'
puerto=8080

ftpdir='/tmp'

def https(alarma):
    class GetHandler(BaseHTTPRequestHandler):    
        def do_GET(self):
            parsed_path = parse.urlparse(self.path)            
            print(parsed_path)

            self.send_response(200)    
            self.send_header('Content-Type', 'text/plain; charset=utf-8')
            self.end_headers()
            self.wfile.write("OK GOT IT".encode("utf-8"))
            alarma.retval=1

    server = HTTPServer((serverip, puerto), GetHandler)
    server.socket = ssl.wrap_socket (server.socket, 
                                     keyfile="../71407457_www.example.com.key", 
                                     certfile='../71407457_www.example.com.cert', server_side=True)

    threading.Thread(target=server.serve_forever).start()
    return server

class MyHandler(FTPHandler):

        def on_connect(self):
            print(self.remote_ip)

def ftpserver(alarma):

    authorizer = DummyAuthorizer()
    authorizer.add_user('user', '1234', ftpdir, perm='wme')

    handler = MyHandler
    handler.authorizer = authorizer

    handler.banner = " : "
    handler.alarma=alarma

    address = ('', 2122)
    server = FTPServer(address, handler)

    threading.Thread(target=server.serve_forever).start()
    return server

def mycall6(channel):
    print(channel)
def mycall19(channel):
    print(channel)

def mycall21(channel):
    print(channel)

def mycall13(channel):
    print(channel)

class alarma():
 def __init__(self,pin):

        sensor6=MotionSensor(6)
        sensor6.when_motion=mycall6
        sensor19=MotionSensor(19)
        sensor19.when_motion=mycall19
        sensor21=MotionSensor(21)
        sensor21.when_motion=mycall21
        sensor13=MotionSensor(13)
        sensor13.when_motion=mycall13

def main():

    a=alarma(6) 
    #serverhttps=https(a)
    #serverftp=ftpserver(a)  

    while True:
        #signal.pause()
        time.sleep(15)
        print("ping")

if __name__ == '__main__':
        main()`
littlegreenbean33 commented 6 years ago

BTW you see the server commented for testing purposes. Both httpserver and ftpserver will meddle with the callbacks