<s> from httplib import HTTPConnection <EOL> has_https = False <EOL> try : <EOL> from httplib import HTTPSConnection <EOL> has_https = True <EOL> except ImportError : <EOL> pass <EOL> from urllib import urlencode <EOL> from urlparse import urlparse <EOL> try : <EOL> from socket import gethostname , getdefaulttimeout , setdefaulttimeout <EOL> except ImportError : <EOL> def gethostname ( ) : return "<STR_LIT>" <EOL> def getdefaulttimeout ( ) : return <NUM_LIT> <EOL> def setdefaulttimeout ( num ) : pass <EOL> from email . Utils import formatdate <EOL> import smtplib <EOL> import simplejson <EOL> keys = [ "<STR_LIT>" , "<STR_LIT:count>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT:type>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT:url>" , "<STR_LIT:status>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT:username>" ] <EOL> default_route = "<STR_LIT>" <EOL> class post : <EOL> def __init__ ( self ) : <EOL> self . _data = { } <EOL> self . transport = "<STR_LIT:http>" <EOL> self . smtp_server = "<STR_LIT:localhost>" <EOL> self . smtp_from = "<STR_LIT>" <EOL> self . url = None <EOL> self . smtp_to = None <EOL> self . set ( "<STR_LIT>" , gethostname ( ) ) <EOL> self . set ( "<STR_LIT>" , formatdate ( ) ) <EOL> def set ( self , key , value ) : <EOL> """<STR_LIT>""" <EOL> if key not in keys : <EOL> raise ValueError , "<STR_LIT>" % key <EOL> self . _data [ key ] = value <EOL> def server ( self , url = None , email = None ) : <EOL> """<STR_LIT>""" <EOL> if url : self . url = urlparse ( url ) <EOL> if email : self . smtp_to = email <EOL> def send ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _send ( ) <EOL> def as_json ( self ) : <EOL> return simplejson . dumps ( self . _data ) <EOL> def _data_encoded ( self ) : <EOL> data = { } <EOL> for k in keys : <EOL> if self . _data . get ( k ) : <EOL> data [ k ] = self . _data . get ( k ) <EOL> return urlencode ( data ) <EOL> def _send ( self ) : <EOL> key = self . transport . lower ( ) <EOL> assert key in [ "<STR_LIT:http>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> if key in [ "<STR_LIT:http>" , "<STR_LIT>" ] : <EOL> assert self . url , "<STR_LIT>" <EOL> self . _send_http ( ) <EOL> elif key == "<STR_LIT>" : <EOL> assert self . smtp_to , "<STR_LIT>" <EOL> self . _send_smtp ( ) <EOL> def _msg_body ( self ) : <EOL> msg = "<STR_LIT>" % ( self . smtp_from , self . smtp_to , self . as_json ( ) ) <EOL> return msg <EOL> def _send_smtp ( self ) : <EOL> msg = self . _msg_body ( ) <EOL> s = smtplib . SMTP ( self . smtp_server ) <EOL> s . sendmail ( self . smtp_from , self . smtp_to , msg ) <EOL> s . quit ( ) <EOL> def _send_http ( self ) : <EOL> if self . transport == "<STR_LIT>" and has_https : <EOL> h = HTTPSConnection ( self . url [ <NUM_LIT:1> ] ) <EOL> else : <EOL> h = HTTPConnection ( self . url [ <NUM_LIT:1> ] ) <EOL> headers = { <EOL> "<STR_LIT>" : '<STR_LIT>' , <EOL> "<STR_LIT>" : "<STR_LIT>" } <EOL> data = self . _data_encoded ( ) <EOL> oldtimeout = getdefaulttimeout ( ) <EOL> try : <EOL> setdefaulttimeout ( <NUM_LIT:10> ) <EOL> h . request ( "<STR_LIT:POST>" , default_route , data , headers ) <EOL> reply = h . getresponse ( ) <EOL> if reply . status != <NUM_LIT:200> : <EOL> raise ValueError , "<STR_LIT>" % ( reply . read ( ) , reply . status ) <EOL> finally : <EOL> setdefaulttimeout ( oldtimeout ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> new = post ( ) <EOL> new . server ( url = "<STR_LIT>" ) <EOL> new . set ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> new . set ( "<STR_LIT>" , <NUM_LIT:4> ) <EOL> new . set ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> new . set ( "<STR_LIT:url>" , "<STR_LIT>" ) <EOL> new . set ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> new . set ( "<STR_LIT>" , "<STR_LIT:127.0.0.1>" ) <EOL> new . set ( "<STR_LIT:type>" , "<STR_LIT>" ) <EOL> new . set ( "<STR_LIT:status>" , "<STR_LIT>" ) <EOL> new . set ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> new . set ( "<STR_LIT>" , """<STR_LIT>""" ) <EOL> new . set ( "<STR_LIT:username>" , "<STR_LIT>" ) <EOL> new . set ( "<STR_LIT>" , """<STR_LIT>""" ) <EOL> new . set ( "<STR_LIT>" , """<STR_LIT>""" ) <EOL> new . send ( ) </s>
