Ir al contenido principal

Ralsina.Me — El sitio web de Roberto Alsina

Publicaciones sobre programming (publicaciones antiguas, página 31)

Linux ayuda a Windows: Mandar SMS

Es­toy se­gu­ro que tie­ne que ha­ber al­gu­na ma­ne­ra de ha­cer­lo an­da­r. Por otro la­do, ya lo te­nía an­dan­do en Li­nu­x... así que uno pue­de sim­ple­men­te usar es­to en un Li­nux ami­go, y man­dar men­sa­jes SMS ac­ce­dien­do a una URL es­pe­cia­l:

#!/usr/bin/env python
from colubrid import BaseApplication, HttpResponse, execute
import os

class SMSApplication(BaseApplication):

  def process_request(self):
      numero = self.request.args.get('numero')
      mensaje = self.request.args.get('mensaje')
      [entrada,salida]=os.popen4('/usr/bin/gnokii --sendsms %s'%numero,mode='rw')
      entrada.write(mensaje)
      entrada.flush()
      entrada.close()
      msg=salida.read()
      response = HttpResponse(msg)
      response['Content-Type'] = 'text/plain'
      return response

if __name__ == '__main__':
  execute(SMSApplication,debug=True, hostname='mybox.domain.internal', port=8080,reload=True)

Si alguien abre http://­m­y­bo­x.­do­mai­n.in­ter­na­l:8080/?­nu­me­ro­=1234?­men­sa­je=ho­la%20­mun­do manda "hola mundo" al numero 1234.

Su­pon­go que po­dría de­cir que es un ser­vi­cio web de te­le­fo­nía, pe­ro es la so­lu­ción de 5 mi­nu­tos que se me ocu­rrió.

Usa una co­sa que se lla­ma co­lu­brid que no es real­men­te un fra­mewo­rk web y no al­go co­no­ci­do por­que que­ría man­te­ner­lo sim­ple, y no se po­ne mu­cho más sim­ple que es­to.

A simple memcache memoizer for python>=2.2

Just a snip­pet of code be­cause ev­ery once in a while I need some­thing like the clas­sic mem­o­ize dec­o­ra­tor but am work­ing on a Cen­tOS 4 bix (with python 2.3!)

I am still test­ing it, and am not even sure it re­al­ly work­s, but it should be close.

cache=memcache.Client(['127.0.0.1:11211'], debug=0)
cachetimeout=30

def memoize(fun):
    def inner(*args, **kwargs):
        key=repr(fun)+repr(args)+repr(kwargs)
        cached = cache.get(key)
        if cached is None:
            val = fun(*args, **kwargs)
            print "Setting: ",key, "to: ",val
            cache.set(key,val,cachetimeout)
            return val
        return cached
    return inner

And lat­er in­side a class:

def myfun(self,arg):
   :
   :
myfun=memoize(myfun)

And that's it. The ba­sic idea I stole from a blog who was in­spired by a Paul Gra­ham book. It can be triv­ial­ly turned in­to a dec­o­ra­tor, of course (but then on­ly works on 2.4 and lat­er).

Playing with GIT

The guys at http://github.­com have been nice enough to add me to their be­ta pro­gram, so I am do­ing a lit­tle project there, to fig­ure out if I like git or not.

Since ev­ery­one raves about it, I sup­pose I will, and then will have to turn my nu­mer­ous google­code SVN re­pos in­to git mir­rors or what­ev­er the cor­rect ter­mi­nol­o­gy is.

New qmail plugin idea: overload

But then you start seeing how your "not pre­pro­ce­ss­e­d" queue star­ts gro­win­g, and gro­win­g...

This can al­so mean things like cla­mav or spa­ma­ssas­si­n, whi­ch need to che­ck the mail be­fo­re it ge­ts queued are not kee­ping up wi­th the mail flo­w, or ma­y­be so­me IO per­for­ma­ce is­sue.

But what can you do ri­gh now to fix it?

We­ll, you can di­sa­ble spa­ma­ssas­si­n, or, in ex­tre­me ca­ses, shu­tdo­wn SM­TP so the sys­tem has a chan­ce to ca­tch its brea­th so to speak.

Of cour­se, clo­sing SM­TP means your own users can't send email ei­the­r, whi­ch su­cks.

Now the­re is a li­gh­ter al­ter­na­ti­ve: shu­tdo­wn SM­TP for tho­se who are not your user­s.

He­re's the tri­vial co­de, im­ple­men­ted as a SPP plu­gi­n:

#!/bin/dash

if [ -f /var/qmail/control/overloaded ]
then
      if [ -z "$SMTPAUTHUSER" ]
      then
              echo R451 Temporary Failure: Server overload
              echo overload: $PPID Temporary Failure: Server overload >&2
      fi
fi

And if you are da­ring and want to make your sys­tem se­l­f-­co­rrec­tin­g, ma­y­be you should cron so­me­thing like this:

* * * * * if [ `qmail-qstat  | tail -1 | cut -d: -f2` -gt 100 ];\
then touch /var/qmail/control/overloaded ;\
else rm -f /var/qmail/control/overloaded; fi

I wi­ll pro­ba­bly co­de it again in C and make it part of ra/­plu­gin­s.

Extraño bug con Django/PyODB

Pa­ra lo­guear un usua­rio se pue­de ha­cer al­go así:

[root@wally app]# python manage.py shell
Python 2.3.4 (#1, May  2 2007, 19:26:00)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from pyodb import *
>>> import django.contrib.auth
>>> print django.contrib.auth.authenticate(username='user',password='pass')
user

Y des­pués se usa el ob­je­to user pa­ra djan­go­.­con­tri­b.au­th.­lo­gin

Una par­te de los da­tos de au­ten­ti­ca­ción es­tán dis­po­ni­bles en un Mi­cro­so­ft SQL Ser­ve­r, y los ob­ten­go via pyo­db. Y es­to es lo que su­ce­de cuan­do tra­to de au­ten­ti­car el usua­rio (a­su­ma­mos que user y pa­ss son vá­li­do­s, es­to es el frag­men­to más pe­que­ño que dis­pa­ra el bu­g):

[root@wally app]# python manage.py shell
Python 2.3.4 (#1, May  2 2007, 19:26:00)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from pyodb import Connect
>>> import django.contrib.auth
>>> c1=Connect("moodle_emisql",uid="gruposintramed",pwd="yev431")
>>> print django.contrib.auth.authenticate(username='user',password='pass')
None

Com pue­den ve­r, lo del pyo­db no de­be­ría in­ter­fe­rir con lo de djan­go. Im­por­to so­lo una fun­ción y co­nec­to con la BB­DD, ni si­quie­ra eje­cu­to na­da de SQ­L, no uso nin­gún da­to bte­ni­do de la co­ne­xión SQ­L. Pe­ro las lla­ma­das a au­then­ti­ca­te fa­llan.

¿Co­mo se su­po­ne que uno de­pu­ra es­to? Me las arre­glé mo­vien­do lo de PyO­DB a otro mó­du­lo pe­ro es ra­ro.


Contents © 2000-2023 Roberto Alsina