import python with __main__ method - python
I have a python script that have __main__ statement and took all values parametric.
I want to import and use it in my own script.
Actually I can import but don't know how to use it.
As you see below, __main__ is a bit complicated and rewriting it will take time because I even don't know what does most of code mean.
Want to know is there any way to import and use the code as a function?
import os
import sys
import time
import base64
from urllib2 import urlopen
from urllib2 import Request
from urllib2 import HTTPError
from urllib import urlencode
from urllib import quote
from exceptions import Exception
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.application import MIMEApplication
from email.encoders import encode_noop
from api_util import json2python, python2json
class MalformedResponse(Exception):
pass
class RequestError(Exception):
pass
class Client(object):
default_url = 'http://nova.astrometry.net/api/'
def __init__(self,
apiurl = default_url):
self.session = None
self.apiurl = apiurl
def get_url(self, service):
return self.apiurl + service
def send_request(self, service, args={}, file_args=None):
'''
service: string
args: dict
'''
if self.session is not None:
args.update({ 'session' : self.session })
print 'Python:', args
json = python2json(args)
print 'Sending json:', json
url = self.get_url(service)
print 'Sending to URL:', url
# If we're sending a file, format a multipart/form-data
if file_args is not None:
m1 = MIMEBase('text', 'plain')
m1.add_header('Content-disposition', 'form-data; name="request-json"')
m1.set_payload(json)
m2 = MIMEApplication(file_args[1],'octet-stream',encode_noop)
m2.add_header('Content-disposition',
'form-data; name="file"; filename="%s"' % file_args[0])
#msg.add_header('Content-Disposition', 'attachment',
# filename='bud.gif')
#msg.add_header('Content-Disposition', 'attachment',
# filename=('iso-8859-1', '', 'FuSballer.ppt'))
mp = MIMEMultipart('form-data', None, [m1, m2])
# Makie a custom generator to format it the way we need.
from cStringIO import StringIO
from email.generator import Generator
class MyGenerator(Generator):
def __init__(self, fp, root=True):
Generator.__init__(self, fp, mangle_from_=False,
maxheaderlen=0)
self.root = root
def _write_headers(self, msg):
# We don't want to write the top-level headers;
# they go into Request(headers) instead.
if self.root:
return
# We need to use \r\n line-terminator, but Generator
# doesn't provide the flexibility to override, so we
# have to copy-n-paste-n-modify.
for h, v in msg.items():
print >> self._fp, ('%s: %s\r\n' % (h,v)),
# A blank line always separates headers from body
print >> self._fp, '\r\n',
# The _write_multipart method calls "clone" for the
# subparts. We hijack that, setting root=False
def clone(self, fp):
return MyGenerator(fp, root=False)
fp = StringIO()
g = MyGenerator(fp)
g.flatten(mp)
data = fp.getvalue()
headers = {'Content-type': mp.get('Content-type')}
if False:
print 'Sending headers:'
print ' ', headers
print 'Sending data:'
print data[:1024].replace('\n', '\\n\n').replace('\r', '\\r')
if len(data) > 1024:
print '...'
print data[-256:].replace('\n', '\\n\n').replace('\r', '\\r')
print
else:
# Else send x-www-form-encoded
data = {'request-json': json}
print 'Sending form data:', data
data = urlencode(data)
print 'Sending data:', data
headers = {}
request = Request(url=url, headers=headers, data=data)
try:
f = urlopen(request)
txt = f.read()
print 'Got json:', txt
result = json2python(txt)
print 'Got result:', result
stat = result.get('status')
print 'Got status:', stat
if stat == 'error':
errstr = result.get('errormessage', '(none)')
raise RequestError('server error message: ' + errstr)
return result
except HTTPError, e:
print 'HTTPError', e
txt = e.read()
open('err.html', 'wb').write(txt)
print 'Wrote error text to err.html'
def login(self, apikey):
args = { 'apikey' : apikey }
result = self.send_request('login', args)
sess = result.get('session')
print 'Got session:', sess
if not sess:
raise RequestError('no session in result')
self.session = sess
def _get_upload_args(self, **kwargs):
args = {}
for key,default,typ in [('allow_commercial_use', 'd', str),
('allow_modifications', 'd', str),
('publicly_visible', 'y', str),
('scale_units', None, str),
('scale_type', None, str),
('scale_lower', None, float),
('scale_upper', None, float),
('scale_est', None, float),
('scale_err', None, float),
('center_ra', None, float),
('center_dec', None, float),
('radius', None, float),
('downsample_factor', None, int),
('tweak_order', None, int),
('crpix_center', None, bool),
# image_width, image_height
]:
if key in kwargs:
val = kwargs.pop(key)
val = typ(val)
args.update({key: val})
elif default is not None:
args.update({key: default})
print 'Upload args:', args
return args
def url_upload(self, url, **kwargs):
args = dict(url=url)
args.update(self._get_upload_args(**kwargs))
result = self.send_request('url_upload', args)
return result
def upload(self, fn, **kwargs):
args = self._get_upload_args(**kwargs)
try:
f = open(fn, 'rb')
result = self.send_request('upload', args, (fn, f.read()))
return result
except IOError:
print 'File %s does not exist' % fn
raise
def submission_images(self, subid):
result = self.send_request('submission_images', {'subid':subid})
return result.get('image_ids')
def overlay_plot(self, service, outfn, wcsfn, wcsext=0):
from astrometry.util import util as anutil
wcs = anutil.Tan(wcsfn, wcsext)
params = dict(crval1 = wcs.crval[0], crval2 = wcs.crval[1],
crpix1 = wcs.crpix[0], crpix2 = wcs.crpix[1],
cd11 = wcs.cd[0], cd12 = wcs.cd[1],
cd21 = wcs.cd[2], cd22 = wcs.cd[3],
imagew = wcs.imagew, imageh = wcs.imageh)
result = self.send_request(service, {'wcs':params})
print 'Result status:', result['status']
plotdata = result['plot']
plotdata = base64.b64decode(plotdata)
open(outfn, 'wb').write(plotdata)
print 'Wrote', outfn
def sdss_plot(self, outfn, wcsfn, wcsext=0):
return self.overlay_plot('sdss_image_for_wcs', outfn,
wcsfn, wcsext)
def galex_plot(self, outfn, wcsfn, wcsext=0):
return self.overlay_plot('galex_image_for_wcs', outfn,
wcsfn, wcsext)
def myjobs(self):
result = self.send_request('myjobs/')
return result['jobs']
def job_status(self, job_id, justdict=False):
result = self.send_request('jobs/%s' % job_id)
if justdict:
return result
stat = result.get('status')
if stat == 'success':
result = self.send_request('jobs/%s/calibration' % job_id)
print 'Calibration:', result
result = self.send_request('jobs/%s/tags' % job_id)
print 'Tags:', result
result = self.send_request('jobs/%s/machine_tags' % job_id)
print 'Machine Tags:', result
result = self.send_request('jobs/%s/objects_in_field' % job_id)
print 'Objects in field:', result
result = self.send_request('jobs/%s/annotations' % job_id)
print 'Annotations:', result
result = self.send_request('jobs/%s/info' % job_id)
print 'Calibration:', result
return stat
def sub_status(self, sub_id, justdict=False):
result = self.send_request('submissions/%s' % sub_id)
if justdict:
return result
return result.get('status')
def jobs_by_tag(self, tag, exact):
exact_option = 'exact=yes' if exact else ''
result = self.send_request(
'jobs_by_tag?query=%s&%s' % (quote(tag.strip()), exact_option),
{},
)
return result
if __name__ == '__main__':
import optparse
parser = optparse.OptionParser()
parser.add_option('--server', dest='server', default=Client.default_url,
help='Set server base URL (eg, %default)')
parser.add_option('--apikey', '-k', dest='apikey',
help='API key for Astrometry.net web service; if not given will check AN_API_KEY environment variable')
parser.add_option('--upload', '-u', dest='upload', help='Upload a file')
parser.add_option('--wait', '-w', dest='wait', action='store_true', help='After submitting, monitor job status')
parser.add_option('--wcs', dest='wcs', help='Download resulting wcs.fits file, saving to given filename; implies --wait if --urlupload or --upload')
parser.add_option('--kmz', dest='kmz', help='Download resulting kmz file, saving to given filename; implies --wait if --urlupload or --upload')
parser.add_option('--urlupload', '-U', dest='upload_url', help='Upload a file at specified url')
parser.add_option('--scale-units', dest='scale_units',
choices=('arcsecperpix', 'arcminwidth', 'degwidth', 'focalmm'), help='Units for scale estimate')
#parser.add_option('--scale-type', dest='scale_type',
# choices=('ul', 'ev'), help='Scale bounds: lower/upper or estimate/error')
parser.add_option('--scale-lower', dest='scale_lower', type=float, help='Scale lower-bound')
parser.add_option('--scale-upper', dest='scale_upper', type=float, help='Scale upper-bound')
parser.add_option('--scale-est', dest='scale_est', type=float, help='Scale estimate')
parser.add_option('--scale-err', dest='scale_err', type=float, help='Scale estimate error (in PERCENT), eg "10" if you estimate can be off by 10%')
parser.add_option('--ra', dest='center_ra', type=float, help='RA center')
parser.add_option('--dec', dest='center_dec', type=float, help='Dec center')
parser.add_option('--radius', dest='radius', type=float, help='Search radius around RA,Dec center')
parser.add_option('--downsample', dest='downsample_factor', type=int, help='Downsample image by this factor')
parser.add_option('--parity', dest='parity', choices=('0','1'), help='Parity (flip) of image')
parser.add_option('--tweak-order', dest='tweak_order', type=int, help='SIP distortion order (default: 2)')
parser.add_option('--crpix-center', dest='crpix_center', action='store_true', default=None, help='Set reference point to center of image?')
parser.add_option('--sdss', dest='sdss_wcs', nargs=2, help='Plot SDSS image for the given WCS file; write plot to given PNG filename')
parser.add_option('--galex', dest='galex_wcs', nargs=2, help='Plot GALEX image for the given WCS file; write plot to given PNG filename')
parser.add_option('--substatus', '-s', dest='sub_id', help='Get status of a submission')
parser.add_option('--jobstatus', '-j', dest='job_id', help='Get status of a job')
parser.add_option('--jobs', '-J', dest='myjobs', action='store_true', help='Get all my jobs')
parser.add_option('--jobsbyexacttag', '-T', dest='jobs_by_exact_tag', help='Get a list of jobs associated with a given tag--exact match')
parser.add_option('--jobsbytag', '-t', dest='jobs_by_tag', help='Get a list of jobs associated with a given tag')
parser.add_option( '--private', '-p',
dest='public',
action='store_const',
const='n',
default='y',
help='Hide this submission from other users')
parser.add_option('--allow_mod_sa','-m',
dest='allow_mod',
action='store_const',
const='sa',
default='d',
help='Select license to allow derivative works of submission, but only if shared under same conditions of original license')
parser.add_option('--no_mod','-M',
dest='allow_mod',
action='store_const',
const='n',
default='d',
help='Select license to disallow derivative works of submission')
parser.add_option('--no_commercial','-c',
dest='allow_commercial',
action='store_const',
const='n',
default='d',
help='Select license to disallow commercial use of submission')
opt,args = parser.parse_args()
if opt.apikey is None:
# try the environment
opt.apikey = os.environ.get('AN_API_KEY', None)
if opt.apikey is None:
parser.print_help()
print
print 'You must either specify --apikey or set AN_API_KEY'
sys.exit(-1)
args = {}
args['apiurl'] = opt.server
c = Client(**args)
c.login(opt.apikey)
if opt.upload or opt.upload_url:
if opt.wcs or opt.kmz:
opt.wait = True
kwargs = dict(
allow_commercial_use=opt.allow_commercial,
allow_modifications=opt.allow_mod,
publicly_visible=opt.public)
if opt.scale_lower and opt.scale_upper:
kwargs.update(scale_lower=opt.scale_lower,
scale_upper=opt.scale_upper,
scale_type='ul')
elif opt.scale_est and opt.scale_err:
kwargs.update(scale_est=opt.scale_est,
scale_err=opt.scale_err,
scale_type='ev')
elif opt.scale_lower or opt.scale_upper:
kwargs.update(scale_type='ul')
if opt.scale_lower:
kwargs.update(scale_lower=opt.scale_lower)
if opt.scale_upper:
kwargs.update(scale_upper=opt.scale_upper)
for key in ['scale_units', 'center_ra', 'center_dec', 'radius',
'downsample_factor', 'tweak_order', 'crpix_center',]:
if getattr(opt, key) is not None:
kwargs[key] = getattr(opt, key)
if opt.parity is not None:
kwargs.update(parity=int(opt.parity))
if opt.upload:
upres = c.upload(opt.upload, **kwargs)
if opt.upload_url:
upres = c.url_upload(opt.upload_url, **kwargs)
stat = upres['status']
if stat != 'success':
print 'Upload failed: status', stat
print upres
sys.exit(-1)
opt.sub_id = upres['subid']
if opt.wait:
if opt.job_id is None:
if opt.sub_id is None:
print "Can't --wait without a submission id or job id!"
sys.exit(-1)
while True:
stat = c.sub_status(opt.sub_id, justdict=True)
print 'Got status:', stat
jobs = stat.get('jobs', [])
if len(jobs):
for j in jobs:
if j is not None:
break
if j is not None:
print 'Selecting job id', j
opt.job_id = j
break
time.sleep(5)
success = False
while True:
stat = c.job_status(opt.job_id, justdict=True)
print 'Got job status:', stat
if stat.get('status','') in ['success']:
success = (stat['status'] == 'success')
break
time.sleep(5)
if success:
c.job_status(opt.job_id)
# result = c.send_request('jobs/%s/calibration' % opt.job_id)
# print 'Calibration:', result
# result = c.send_request('jobs/%s/tags' % opt.job_id)
# print 'Tags:', result
# result = c.send_request('jobs/%s/machine_tags' % opt.job_id)
# print 'Machine Tags:', result
# result = c.send_request('jobs/%s/objects_in_field' % opt.job_id)
# print 'Objects in field:', result
#result = c.send_request('jobs/%s/annotations' % opt.job_id)
#print 'Annotations:', result
retrieveurls = []
if opt.wcs:
# We don't need the API for this, just construct URL
url = opt.server.replace('/api/', '/wcs_file/%i' % opt.job_id)
retrieveurls.append((url, opt.wcs))
if opt.kmz:
url = opt.server.replace('/api/', '/kml_file/%i/' % opt.job_id)
retrieveurls.append((url, opt.kmz))
for url,fn in retrieveurls:
print 'Retrieving file from', url, 'to', fn
f = urlopen(url)
txt = f.read()
w = open(fn, 'wb')
w.write(txt)
w.close()
print 'Wrote to', fn
opt.job_id = None
opt.sub_id = None
if opt.sdss_wcs:
(wcsfn, outfn) = opt.sdss_wcs
c.sdss_plot(outfn, wcsfn)
if opt.galex_wcs:
(wcsfn, outfn) = opt.galex_wcs
c.galex_plot(outfn, wcsfn)
if opt.sub_id:
print c.sub_status(opt.sub_id)
if opt.job_id:
print c.job_status(opt.job_id)
#result = c.send_request('jobs/%s/annotations' % opt.job_id)
#print 'Annotations:', result
if opt.jobs_by_tag:
tag = opt.jobs_by_tag
print c.jobs_by_tag(tag, None)
if opt.jobs_by_exact_tag:
tag = opt.jobs_by_exact_tag
print c.jobs_by_tag(tag, 'yes')
if opt.myjobs:
jobs = c.myjobs()
print jobs
#print c.submission_images(1)
No, there is no clean way to do so. When the module is being imported, it's code is executed and all global variables are set as attributes to the module object. So if part of the code is not executed at all (is guarded by __main__ condition) there is no clean way to get access to that code. You can however run code of this module with substituted __name__ but that's very hackish.
You should refactor this module and move whole __main__ part into a method and call it like this:
def main():
do_everything()
if __name__ == '__main__':
main()
This way consumer apps will be able to run code without having to run it in a separate process.
Use the runpy module in the Python 3 Standard Library
See that data can be passed to and from the called script
# top.py
import runpy
import sys
sys.argv += ["another parameter"]
module_globals_dict = runpy.run_path("other_script.py",
init_globals = globals(), run_name="__main__")
print(module_globals_dict["return_value"])
# other_script.py
# Note we did not load sys module, it gets passed to this script
script_name = sys.argv[0]
print(f"Script {script_name} loaded")
if __name__ == "__main__":
params = sys.argv[1:]
print(f"Script {script_name} run with params: {params}")
return_value = f"{script_name} Done"
by what your saying you want to call a function in the script that is importing the module so try:
import __main__
__main__.myfunc()
Related
How to use Wave file as input in VOSK speech recognition?
I have a project that needs to get a recorded file and then process by the code and extract the text from file and match the extracted file with the other text and verify it. my problem is: I can't use recorded file in code and it does'nt read the file init function is the fundamental of code. verify functtion confirm the matched speech and text. import argparse import json import os import queue import random import sys from difflib import SequenceMatcher import numpy as np import sounddevice as sd import vosk q = queue.Queue() def int_or_str(text): """Helper function for argument parsing.""" try: return int(text) except ValueError: return text def callback(indata, frames, time, status): """This is called (from a separate thread) for each audio block.""" if status: print(status, file=sys.stderr) q.put(bytes(indata)) def init(): parser = argparse.ArgumentParser(add_help=False) parser.add_argument( '-l', '--list-devices', action='store_true', help='show list of audio devices and exit') args, remaining = parser.parse_known_args() if args.list_devices: print(sd.query_devices()) parser.exit(0) parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter, parents=[parser]) parser.add_argument( '-f', '--filename', type=str, metavar='FILENAME', help='audio file to store recording to') parser.add_argument( '-m', '--model', type=str, metavar='MODEL_PATH', help='Path to the model') parser.add_argument( '-d', '--device', type=int_or_str, help='input device (numeric ID or substring)') parser.add_argument( '-r', '--samplerate', type=int, help='sampling rate') args = parser.parse_args(remaining) try: if args.model is None: args.model = "model" if not os.path.exists(args.model): print("Please download a model for your language from https://alphacephei.com/vosk/models") print("and unpack as 'model' in the current folder.") parser.exit(0) if args.samplerate is None: device_info = sd.query_devices(args.device, 'input') # soundfile expects an int, sounddevice provides a float: args.samplerate = int(device_info['default_samplerate']) model = vosk.Model(args.model) if args.filename: dump_fn = open(args.filename, "wb") else: dump_fn = None except KeyboardInterrupt: print('\nDone') parser.exit(0) except Exception as e: parser.exit(type(e).__name__ + ': ' + str(e)) return model, args def verify(random_sentence, model, args): num, T_num, F_num, num_word = 0, 0, 0, 1 with sd.RawInputStream(samplerate=args.samplerate, blocksize=8000, device=args.device, dtype='int16', channels=1, callback=callback): rec = vosk.KaldiRecognizer(model, args.samplerate) print("{}) ".format(num_word), random_sentence, end='\n') print('=' * 30, end='\n') run = True while run: data = q.get() if rec.AcceptWaveform(data): res = json.loads(rec.FinalResult()) res['text'] = res['text'].replace('ي', 'ی') if SequenceMatcher(None, random_sentence, res['text']).ratio() > 0.65: T_num, num, num_word += 1 else: F_num, num, num_word += 1 run = False print('=' * 30) print('True Cases : {}\n False Cases : {}'.format(T_num, F_num)) if __name__ == "__main__": model, args = init() verify(random_sentences, model, args)
I have been working on a similar project. I modified the code from VOSK Git repo and wrote the following function that takes file name / path as the input and outputs the captured text. Sometimes, when there is a long pause (~seconds) in the audio file, the returned text would be an empty string. To remedy this problem, I had to write additional code that picks out the longest string that was captured. I could make do with this fix. def get_text_from_voice(filename): if not os.path.exists("model"): print ("Please download the model from https://alphacephei.com/vosk/models and unpack as 'model' in the current folder.") exit (1) wf = wave.open(filename, "rb") if wf.getnchannels() != 1 or wf.getsampwidth() != 2 or wf.getcomptype() != "NONE": print ("Audio file must be WAV format mono PCM.") exit (1) model = Model("model") rec = KaldiRecognizer(model, wf.getframerate()) rec.SetWords(True) text_lst =[] p_text_lst = [] p_str = [] len_p_str = [] while True: data = wf.readframes(4000) if len(data) == 0: break if rec.AcceptWaveform(data): text_lst.append(rec.Result()) print(rec.Result()) else: p_text_lst.append(rec.PartialResult()) print(rec.PartialResult()) if len(text_lst) !=0: jd = json.loads(text_lst[0]) txt_str = jd["text"] elif len(p_text_lst) !=0: for i in range(0,len(p_text_lst)): temp_txt_dict = json.loads(p_text_lst[i]) p_str.append(temp_txt_dict['partial']) len_p_str = [len(p_str[j]) for j in range(0,len(p_str))] max_val = max(len_p_str) indx = len_p_str.index(max_val) txt_str = p_str[indx] else: txt_str ='' return txt_str Make sure that the correct model is present in the same directory or put in the path to the model. Also, note that VOSK accepts audio files only in wav mono PCM format.
python tinder bot won't send messages - networking error
I'm putting together a python Tinder bot that auto swipes right, and I also want it to send messages. Everything works properly except that when it matches, it should send a message, but doesn't. I've tried every possible combination of code I can think of, but to no avail. Any help with figuring this out would be greatly appreciate by me and all my future Tinder matches (if I even get any). The code calls a funtcion called talk(user_id) that should perform a POST curl request. Code: 1,1 Top# encoding: utf8 import argparse from datetime import datetime import json from random import randint import requests import sys from time import sleep headers = { 'app_version': '519', 'platform': 'ios', } fb_id = 'XXXXXXXXXXXXX' fb_auth_token = 'XXXXXXXXXXXX' class User(object): def __init__(self, data_dict): self.d = data_dict #property def user_id(self): return self.d['_id'] #property def name(self): return self.d['name'] #property def ago(self): raw = self.d.get('ping_time') if raw: d = datetime.strptime(raw, '%Y-%m-%dT%H:%M:%S.%fZ') secs_ago = int(datetime.now().strftime("%s")) - int(d.strftime("%s")) if secs_ago > 86400: return u'{days} days ago'.format(days=secs_ago / 86400) elif secs_ago < 3600: return u'{mins} mins ago'.format(mins=secs_ago / 60) else: return u'{hours} hours ago'.format(hours=secs_ago / 3600) return '[unknown]' #property def bio(self): try: x = self.d['bio'].encode('ascii', 'ignore').replace('\n', '')[:50].strip() except (UnicodeError, UnicodeEncodeError, UnicodeDecodeError): return '[garbled]' else: return x #property def age(self): raw = self.d.get('birth_date') if raw: d = datetime.strptime(raw, '%Y-%m-%dT%H:%M:%S.%fZ') return datetime.now().year - int(d.strftime('%Y')) return 0 def __unicode__(self): return u'{name} ({age}), {distance}km, {ago}'.format( name=self.d['name'], age=self.age, distance=self.d['distance_mi'], ago=self.ago ) def auth_token(fb_auth_token, fb_user_id): h = headers h.update({'content-type': 'application/json'}) req = requests.post( 'https://api.gotinder.com/auth', headers=h, data=json.dumps({'facebook_token': fb_auth_token, 'facebook_id': fb_user_id}) ) try: return req.json()['token'] except: return None def recommendations(auth_token): h = headers h.update({'X-Auth-Token': auth_token}) r = requests.get('https://api.gotinder.com/user/recs', headers=h) if r.status_code == 401 or r.status_code == 504: raise Exception('Invalid code') print r.content if not 'results' in r.json(): print r.json() for result in r.json()['results']: yield User(result) def like(user_id): try: u = 'https://api.gotinder.com/like/%s' % user_id d = requests.get(u, headers=headers, timeout=0.7).json() except KeyError: raise else: return d['match'] def nope(user_id): try: u = 'https://api.gotinder.com/pass/%s' % user_id requests.get(u, headers=headers, timeout=0.7).json() except KeyError: raise def like_or_nope(): return 'nope' if randint(1, 100) == 31 else 'like' def talk(user_id): try: u = 'https://api.gotinder.com/user/matches/%s' % user_id requests.post( u, data=json.dumps=({'message': 'Hey! How are you?'}) ) except KeyError: raise if __name__ == '__main__': parser = argparse.ArgumentParser(description='Tinder automated bot') parser.add_argument('-l', '--log', type=str, default='activity.log', help='Log file destination') args = parser.parse_args() print 'Tinder bot' print '----------' matches = 0 liked = 0 nopes = 0 while True: token = auth_token(fb_auth_token, fb_id) if not token: print 'could not get token' sys.exit(0) for user in recommendations(token): if not user: break print unicode(user) if user.name == 'Tinder Team': print('Out of swipes, pausing one hour...') sleep(3601) else: try: action = like_or_nope() if action == 'like': print ' -> Like' match = like(user.user_id) if match: print ' -> Match!' conversation = talk(user.user_id) if conversation: print ' -> Message Sent!' with open('./matched.txt', 'a') as m: m.write(user.user_id + u'\n') with open('./liked.txt', 'a') as f: f.write(user.user_id + u'\n') else: print ' -> random nope :(' nope(user.user_id) except: print 'networking error %s' % user.user_id s = float(randint(10000, 20000) / 1000) sleep(s)`
Check the below code if this helps. I used pynder package for dissecting the APIs, but I guess the logic remains the same. session._post('/user/matches/' + match['id'], {"message": "Hey! How are you?"}) Where the post function works like below :- def _post(self, url, data={}): return self._request("post", url, data=data) Try this method data={message} directly It works for me, perfectly fine.
Check server status on Twisted
While I was writing simple message-based fileserver and client, I got the idea about checking fileserver status, but don't know how to realize this: just try to connect and disconnect from server (and how disconnect immediately, when server is not running, if using this way?) or maybe twisted/autobahn have some things, which help to get server status without creating "full connection"? a) fileserver.py import os import sys import json from twisted.internet import reactor from autobahn.twisted.websocket import WebSocketServerFactory, WebSocketServerProtocol, listenWS CONFIG_TEMPLATE = '' CONFIG_DATA = {} class MessageBasedServerProtocol(WebSocketServerProtocol): """ Message-based WebSockets server Template contains some parts as string: [USER_ID:OPERATION_NAME:FILE_ID] - 15 symbols for USER_ID, 10 symbols for OPERATION_NAME, 25 symbols for FILE_ID other - some data """ def __init__(self): path = CONFIG_DATA['path'] base_dir = CONFIG_DATA['base_dir'] # prepare to working with files... if os.path.exists(path) and os.path.isdir(path): os.chdir(path) if not os.path.exists(base_dir) or not os.path.isdir(base_dir): os.mkdir(base_dir) os.chdir(base_dir) else: os.makedir(path) os.chdir(path) os.mkdir(base_dir) os.chdir(base_dir) # init some things self.fullpath = path + '/' + base_dir def __checkUserCatalog(self, user_id): # prepare to working with files... os.chdir(self.fullpath) if not os.path.exists(user_id) or not os.path.isdir(user_id): os.mkdir(user_id) os.chdir(user_id) else: os.chdir(self.fullpath + '/' + user_id) def onOpen(self): print "[USER] User with %s connected" % (self.transport.getPeer()) def connectionLost(self, reason): print '[USER] Lost connection from %s' % (self.transport.getPeer()) def onMessage(self, payload, isBinary): """ Processing request from user and send response """ user_id, cmd, file_id = payload[:54].replace('[', '').replace(']','').split(':') data = payload[54:] operation = "UNK" # WRT - Write, REA -> Read, DEL -> Delete, UNK -> Unknown status = "C" # C -> Complete, E -> Error in operation commentary = 'Succesfull!' # write file into user storage if cmd == 'WRITE_FILE': self.__checkUserCatalog(user_id) operation = "WRT" try: f = open(file_id, "wb") f.write(data) except IOError, argument: status = "E" commentary = argument except Exception, argument: status = "E" commentary = argument raise Exception(argument) finally: f.close() # read some file elif cmd == 'READU_FILE': self.__checkUserCatalog(user_id) operation = "REA" try: f = open(file_id, "rb") commentary = f.read() except IOError, argument: status = "E" commentary = argument except Exception, argument: status = "E" commentary = argument raise Exception(argument) finally: f.close() # delete file from storage (and in main server, in parallel delete from DB) elif cmd == 'DELET_FILE': self.__checkUserCatalog(user_id) operation = "DEL" try: os.remove(file_id) except IOError, argument: status = "E" commentary = argument except Exception, argument: status = "E" commentary = argument raise Exception(argument) self.sendMessage('[%s][%s]%s' % (operation, status, commentary), isBinary=True) if __name__ == '__main__': if len(sys.argv) < 2: print "using python fileserver_client.py [PATH_TO_config.json_FILE]" else: # read config file CONFIG_TEMPLATE = sys.argv[1] with open(CONFIG_TEMPLATE, "r") as f: CONFIG_DATA = json.load(f) # create server factory = WebSocketServerFactory("ws://localhost:9000") factory.protocol = MessageBasedServerProtocol listenWS(factory) reactor.run() b) client.py import json import sys import commands from twisted.internet import reactor from autobahn.twisted.websocket import WebSocketClientFactory, WebSocketClientProtocol, connectWS CONFIG_TEMPLATE = '' CONFIG_DATA = {} class MessageBasedClientProtocol(WebSocketClientProtocol): """ Message-based WebSockets client Template contains some parts as string: [USER_ID:OPERATION_NAME:FILE_ID] - 15 symbols for USER_ID, 10 symbols for OPERATION_NAME, 25 symbols for FILE_ID other - some data """ def onOpen(self): user_id = CONFIG_DATA['user'] operation_name = CONFIG_DATA['cmd'] file_id = CONFIG_DATA['file_id'] src_file = CONFIG_DATA['src_file'] data = '[' + str(user_id) + ':' + str(operation_name) + ':' + str(file_id) + ']' if operation_name == 'WRITE_FILE': with open(src_file, "r") as f: info = f.read() data += str(info) self.sendMessage(data, isBinary=True) def onMessage(self, payload, isBinary): cmd = payload[1:4] result_cmd = payload[6] if cmd in ('WRT', 'DEL'): print payload elif cmd == 'REA': if result_cmd == 'C': try: data = payload[8:] f = open(CONFIG_DATA['src_file'], "wb") f.write(data) except IOError, e: print e except Exception, e: raise Exception(e) finally: print payload[:8] + "Successfully!" f.close() else: print payload reactor.stop() if __name__ == '__main__': if len(sys.argv) < 2: print "using python fileserver_client.py [PATH_TO_config.json_FILE]" else: # read config file CONFIG_TEMPLATE = sys.argv[1] with open(CONFIG_TEMPLATE, "r") as f: CONFIG_DATA = json.load(f) # connection to server factory = WebSocketClientFactory("ws://localhost:9000") factory.protocol = MessageBasedClientProtocol connectWS(factory) reactor.run()
Find solution this issue: using callLater or deferLater for disconnect from server, if can't connect, but when all was 'OK', just take server status, which he says. import sys from twisted.internet.task import deferLater from twisted.internet import reactor from autobahn.twisted.websocket import WebSocketClientFactory, WebSocketClientProtocol, connectWS CONFIG_IP = '' CONFIG_PORT = 9000 def isOffline(status): print status class StatusCheckerProtocol(WebSocketClientProtocol): def __init__(self): self.operation_name = "STATUS_SRV" self.user_id = 'u00000000000000' self.file_id = "000000000000000000000.log" def onOpen(self): data = '[' + str(self.user_id) + ':' + str(self.operation_name) + ':' + str(self.file_id) + ']' self.sendMessage(data, isBinary=True) def onMessage(self, payload, isBinary): cmd = payload[1:4] result_cmd = payload[6] data = payload[8:] print data reactor.stop() if __name__ == '__main__': if len(sys.argv) < 3: print "using python statuschecker.py [IP] [PORT]" else: # read preferences CONFIG_IP = sys.argv[1] CONFIG_PORT = int(sys.argv[2]) server_addr = "ws://%s:%d" % (CONFIG_IP, CONFIG_PORT) # connection to server factory = WebSocketClientFactory(server_addr) factory.protocol = StatusCheckerProtocol connectWS(factory) # create special Deffered, which disconnect us from some server, if can't connect within 3 seconds d = deferLater(reactor, 3, isOffline, 'OFFLINE') d.addCallback(lambda ignored: reactor.stop()) # run all system... reactor.run()
Python: launch default mail client on the system
I'm fairly new to Python and I'm trying to write a plugin for a text editor. I want to know if there is a way to launch default system E-Mail client from python code.
With pywin32: import win32api win32api.ShellExecute(0,'open','mailto:',None,None ,0) Update Ah, I misread your question and presumed you're on Win platform. A platform independent solution would be open mailto link in a browser, like import webbrowser webbrowser.open('mailto:', new=1) Update 2 Some additional research (in fact, first two pages of google search) revealed this excellent snippet: #!/usr/bin/env python '''Utilities for opening files or URLs in the registered default application and for sending e-mail using the user's preferred composer. ''' __version__ = '1.1' __all__ = ['open', 'mailto'] import os import sys import webbrowser import subprocess from email.Utils import encode_rfc2231 _controllers = {} _open = None class BaseController(object): '''Base class for open program controllers.''' def __init__(self, name): self.name = name def open(self, filename): raise NotImplementedError class Controller(BaseController): '''Controller for a generic open program.''' def __init__(self, *args): super(Controller, self).__init__(os.path.basename(args[0])) self.args = list(args) def _invoke(self, cmdline): if sys.platform[:3] == 'win': closefds = False startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW else: closefds = True startupinfo = None if (os.environ.get('DISPLAY') or sys.platform[:3] == 'win' or sys.platform == 'darwin'): inout = file(os.devnull, 'r+') else: # for TTY programs, we need stdin/out inout = None # if possible, put the child precess in separate process group, # so keyboard interrupts don't affect child precess as well as # Python setsid = getattr(os, 'setsid', None) if not setsid: setsid = getattr(os, 'setpgrp', None) pipe = subprocess.Popen(cmdline, stdin=inout, stdout=inout, stderr=inout, close_fds=closefds, preexec_fn=setsid, startupinfo=startupinfo) # It is assumed that this kind of tools (gnome-open, kfmclient, # exo-open, xdg-open and open for OSX) immediately exit after lauching # the specific application returncode = pipe.wait() if hasattr(self, 'fixreturncode'): returncode = self.fixreturncode(returncode) return not returncode def open(self, filename): if isinstance(filename, basestring): cmdline = self.args + [filename] else: # assume it is a sequence cmdline = self.args + filename try: return self._invoke(cmdline) except OSError: return False # Platform support for Windows if sys.platform[:3] == 'win': class Start(BaseController): '''Controller for the win32 start progam through os.startfile.''' def open(self, filename): try: os.startfile(filename) except WindowsError: # [Error 22] No application is associated with the specified # file for this operation: '<URL>' return False else: return True _controllers['windows-default'] = Start('start') _open = _controllers['windows-default'].open # Platform support for MacOS elif sys.platform == 'darwin': _controllers['open']= Controller('open') _open = _controllers['open'].open # Platform support for Unix else: import commands # #WARNING: use the private API of the webbrowser module from webbrowser import _iscommand class KfmClient(Controller): '''Controller for the KDE kfmclient program.''' def __init__(self, kfmclient='kfmclient'): super(KfmClient, self).__init__(kfmclient, 'exec') self.kde_version = self.detect_kde_version() def detect_kde_version(self): kde_version = None try: info = commands.getoutput('kde-config --version') for line in info.splitlines(): if line.startswith('KDE'): kde_version = line.split(':')[-1].strip() break except (OSError, RuntimeError): pass return kde_version def fixreturncode(self, returncode): if returncode is not None and self.kde_version > '3.5.4': return returncode else: return os.EX_OK def detect_desktop_environment(): '''Checks for known desktop environments Return the desktop environments name, lowercase (kde, gnome, xfce) or "generic" ''' desktop_environment = 'generic' if os.environ.get('KDE_FULL_SESSION') == 'true': desktop_environment = 'kde' elif os.environ.get('GNOME_DESKTOP_SESSION_ID'): desktop_environment = 'gnome' else: try: info = commands.getoutput('xprop -root _DT_SAVE_MODE') if ' = "xfce4"' in info: desktop_environment = 'xfce' except (OSError, RuntimeError): pass return desktop_environment def register_X_controllers(): if _iscommand('kfmclient'): _controllers['kde-open'] = KfmClient() for command in ('gnome-open', 'exo-open', 'xdg-open'): if _iscommand(command): _controllers[command] = Controller(command) def get(): controllers_map = { 'gnome': 'gnome-open', 'kde': 'kde-open', 'xfce': 'exo-open', } desktop_environment = detect_desktop_environment() try: controller_name = controllers_map[desktop_environment] return _controllers[controller_name].open except KeyError: if _controllers.has_key('xdg-open'): return _controllers['xdg-open'].open else: return webbrowser.open if os.environ.get("DISPLAY"): register_X_controllers() _open = get() def open(filename): '''Open a file or an URL in the registered default application.''' return _open(filename) def _fix_addersses(**kwargs): for headername in ('address', 'to', 'cc', 'bcc'): try: headervalue = kwargs[headername] if not headervalue: del kwargs[headername] continue elif not isinstance(headervalue, basestring): # assume it is a sequence headervalue = ','.join(headervalue) except KeyError: pass except TypeError: raise TypeError('string or sequence expected for "%s", ' '%s found' % (headername, type(headervalue).__name__)) else: translation_map = {'%': '%25', '&': '%26', '?': '%3F'} for char, replacement in translation_map.items(): headervalue = headervalue.replace(char, replacement) kwargs[headername] = headervalue return kwargs def mailto_format(**kwargs): # #TODO: implement utf8 option kwargs = _fix_addersses(**kwargs) parts = [] for headername in ('to', 'cc', 'bcc', 'subject', 'body', 'attach'): if kwargs.has_key(headername): headervalue = kwargs[headername] if not headervalue: continue if headername in ('address', 'to', 'cc', 'bcc'): parts.append('%s=%s' % (headername, headervalue)) else: headervalue = encode_rfc2231(headervalue) # #TODO: check parts.append('%s=%s' % (headername, headervalue)) mailto_string = 'mailto:%s' % kwargs.get('address', '') if parts: mailto_string = '%s?%s' % (mailto_string, '&'.join(parts)) return mailto_string def mailto(address, to=None, cc=None, bcc=None, subject=None, body=None, attach=None): '''Send an e-mail using the user's preferred composer. Open the user's preferred e-mail composer in order to send a mail to address(es) that must follow the syntax of RFC822. Multiple addresses may be provided (for address, cc and bcc parameters) as separate arguments. All parameters provided are used to prefill corresponding fields in the user's e-mail composer. The user will have the opportunity to change any of this information before actually sending the e-mail. address - specify the destination recipient cc - specify a recipient to be copied on the e-mail bcc - specify a recipient to be blindly copied on the e-mail subject - specify a subject for the e-mail body - specify a body for the e-mail. Since the user will be able to make changes before actually sending the e-mail, this can be used to provide the user with a template for the e-mail text may contain linebreaks attach - specify an attachment for the e-mail. file must point to an existing file ''' mailto_string = mailto_format(**locals()) return open(mailto_string) if __name__ == '__main__': from optparse import OptionParser version = '%%prog %s' % __version__ usage = ( '\n\n%prog FILENAME [FILENAME(s)] -- for opening files' '\n\n%prog -m [OPTIONS] ADDRESS [ADDRESS(es)] -- for sending e-mails' ) parser = OptionParser(usage=usage, version=version, description=__doc__) parser.add_option('-m', '--mailto', dest='mailto_mode', default=False, action='store_true', help='set mailto mode. ' 'If not set any other option is ignored') parser.add_option('--cc', dest='cc', help='specify a recipient to be ' 'copied on the e-mail') parser.add_option('--bcc', dest='bcc', help='specify a recipient to be ' 'blindly copied on the e-mail') parser.add_option('--subject', dest='subject', help='specify a subject for the e-mail') parser.add_option('--body', dest='body', help='specify a body for the ' 'e-mail. Since the user will be able to make changes ' 'before actually sending the e-mail, this can be used ' 'to provide the user with a template for the e-mail ' 'text may contain linebreaks') parser.add_option('--attach', dest='attach', help='specify an attachment ' 'for the e-mail. file must point to an existing file') (options, args) = parser.parse_args() if not args: parser.print_usage() parser.exit(1) if options.mailto_mode: if not mailto(args, None, options.cc, options.bcc, options.subject, options.body, options.attach): sys.exit('Unable to open the e-mail client') else: for name in ('cc', 'bcc', 'subject', 'body', 'attach'): if getattr(options, name): parser.error('The "cc", "bcc", "subject", "body" and "attach" ' 'options are only accepten in mailto mode') success = False for arg in args: if not open(arg): print 'Unable to open "%s"' % arg else: success = True sys.exit(success) Enjoy.
The above-mentioned code only works in Python 2. I've modified it to work for Python 3. The "attach" parameter is no longer supported for security reasons. #!/usr/bin/env python3 '''Utilities for opening files or URLs in the registered default application and for sending e-mail using the user's preferred composer. https://stackoverflow.com/a/19779373/3211506 ''' __version__ = '1.1' __all__ = ['open', 'mailto'] import os import sys import webbrowser import subprocess from email.utils import encode_rfc2231 _controllers = {} _open = None fileopen = open class BaseController(object): '''Base class for open program controllers.''' def __init__(self, name): self.name = name def open(self, filename): raise NotImplementedError class Controller(BaseController): '''Controller for a generic open program.''' def __init__(self, *args): super(Controller, self).__init__(os.path.basename(args[0])) self.args = list(args) def _invoke(self, cmdline): if sys.platform[:3] == 'win': closefds = False startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW else: closefds = True startupinfo = None if (os.environ.get('DISPLAY') or sys.platform[:3] == 'win' or sys.platform == 'darwin'): inout = fileopen(os.devnull, 'r+') else: # for TTY programs, we need stdin/out inout = None # if possible, put the child precess in separate process group, # so keyboard interrupts don't affect child precess as well as # Python setsid = getattr(os, 'setsid', None) if not setsid: setsid = getattr(os, 'setpgrp', None) pipe = subprocess.Popen(cmdline, stdin=inout, stdout=inout, stderr=inout, close_fds=closefds, preexec_fn=setsid, startupinfo=startupinfo) # It is assumed that this kind of tools (gnome-open, kfmclient, # exo-open, xdg-open and open for OSX) immediately exit after lauching # the specific application returncode = pipe.wait() if hasattr(self, 'fixreturncode'): returncode = self.fixreturncode(returncode) return not returncode def open(self, filename): if isinstance(filename, str): cmdline = self.args + [filename] else: # assume it is a sequence cmdline = self.args + filename try: return self._invoke(cmdline) except OSError: return False # Platform support for Windows if sys.platform[:3] == 'win': class Start(BaseController): '''Controller for the win32 start progam through os.startfile.''' def open(self, filename): try: os.startfile(filename) except WindowsError: # [Error 22] No application is associated with the specified # file for this operation: '<URL>' return False else: return True _controllers['windows-default'] = Start('start') _open = _controllers['windows-default'].open # Platform support for MacOS elif sys.platform == 'darwin': _controllers['open']= Controller('open') _open = _controllers['open'].open # Platform support for Unix else: import subprocess, stat # #WARNING: use the private API of the webbrowser module # from webbrowser import _iscommand def _isexecutable(cmd): if os.path.isfile(cmd): mode = os.stat(cmd)[stat.ST_MODE] if mode & stat.S_IXUSR or mode & stat.S_IXGRP or mode & stat.S_IXOTH: return True return False def _iscommand(cmd): """Return True if cmd is executable or can be found on the executable search path.""" if _isexecutable(cmd): return True path = os.environ.get("PATH") if not path: return False for d in path.split(os.pathsep): exe = os.path.join(d, cmd) if _isexecutable(exe): return True return False class KfmClient(Controller): '''Controller for the KDE kfmclient program.''' def __init__(self, kfmclient='kfmclient'): super(KfmClient, self).__init__(kfmclient, 'exec') self.kde_version = self.detect_kde_version() def detect_kde_version(self): kde_version = None try: info = subprocess.getoutput('kde-config --version') for line in info.splitlines(): if line.startswith('KDE'): kde_version = line.split(':')[-1].strip() break except (OSError, RuntimeError): pass return kde_version def fixreturncode(self, returncode): if returncode is not None and self.kde_version > '3.5.4': return returncode else: return os.EX_OK def detect_desktop_environment(): '''Checks for known desktop environments Return the desktop environments name, lowercase (kde, gnome, xfce) or "generic" ''' desktop_environment = 'generic' if os.environ.get('KDE_FULL_SESSION') == 'true': desktop_environment = 'kde' elif os.environ.get('GNOME_DESKTOP_SESSION_ID'): desktop_environment = 'gnome' else: try: info = subprocess.getoutput('xprop -root _DT_SAVE_MODE') if ' = "xfce4"' in info: desktop_environment = 'xfce' except (OSError, RuntimeError): pass return desktop_environment def register_X_controllers(): if _iscommand('kfmclient'): _controllers['kde-open'] = KfmClient() for command in ('gnome-open', 'exo-open', 'xdg-open'): if _iscommand(command): _controllers[command] = Controller(command) def get(): controllers_map = { 'gnome': 'gnome-open', 'kde': 'kde-open', 'xfce': 'exo-open', } desktop_environment = detect_desktop_environment() try: controller_name = controllers_map[desktop_environment] return _controllers[controller_name].open except KeyError: if 'xdg-open' in _controllers: return _controllers['xdg-open'].open else: return webbrowser.open if os.environ.get("DISPLAY"): register_X_controllers() _open = get() def open(filename): '''Open a file or an URL in the registered default application.''' return _open(filename) def _fix_addersses(**kwargs): for headername in ('address', 'to', 'cc', 'bcc'): try: headervalue = kwargs[headername] if not headervalue: del kwargs[headername] continue elif not isinstance(headervalue, str): # assume it is a sequence headervalue = ','.join(headervalue) except KeyError: pass except TypeError: raise TypeError('string or sequence expected for "%s", ' '%s found' % (headername, type(headervalue).__name__)) else: translation_map = {'%': '%25', '&': '%26', '?': '%3F'} for char, replacement in list(translation_map.items()): headervalue = headervalue.replace(char, replacement) kwargs[headername] = headervalue return kwargs def mailto_format(**kwargs): # #TODO: implement utf8 option kwargs = _fix_addersses(**kwargs) parts = [] for headername in ('to', 'cc', 'bcc', 'subject', 'body'): if headername in kwargs: headervalue = kwargs[headername] if not headervalue: continue if headername in ('address', 'to', 'cc', 'bcc'): parts.append('%s=%s' % (headername, headervalue)) else: headervalue = encode_rfc2231(headervalue, charset="utf-8")[7:] # #TODO: check parts.append('%s=%s' % (headername, headervalue)) mailto_string = 'mailto:%s' % kwargs.get('address', '') if parts: mailto_string = '%s?%s' % (mailto_string, '&'.join(parts)) return mailto_string def mailto(address, to=None, cc=None, bcc=None, subject=None, body=None): '''Send an e-mail using the user's preferred composer. Open the user's preferred e-mail composer in order to send a mail to address(es) that must follow the syntax of RFC822. Multiple addresses may be provided (for address, cc and bcc parameters) as separate arguments. All parameters provided are used to prefill corresponding fields in the user's e-mail composer. The user will have the opportunity to change any of this information before actually sending the e-mail. address - specify the destination recipient cc - specify a recipient to be copied on the e-mail bcc - specify a recipient to be blindly copied on the e-mail subject - specify a subject for the e-mail body - specify a body for the e-mail. Since the user will be able to make changes before actually sending the e-mail, this can be used to provide the user with a template for the e-mail text may contain linebreaks attach - specify an attachment for the e-mail. file must point to an existing file (UNSUPPORTED) ''' mailto_string = mailto_format(**locals()) return open(mailto_string) if __name__ == '__main__': from optparse import OptionParser version = '%%prog %s' % __version__ usage = ( '\n\n%prog FILENAME [FILENAME(s)] -- for opening files' '\n\n%prog -m [OPTIONS] ADDRESS [ADDRESS(es)] -- for sending e-mails' ) parser = OptionParser(usage=usage, version=version, description=__doc__) parser.add_option('-m', '--mailto', dest='mailto_mode', default=False, action='store_true', help='set mailto mode. ' 'If not set any other option is ignored') parser.add_option('--cc', dest='cc', help='specify a recipient to be ' 'copied on the e-mail') parser.add_option('--bcc', dest='bcc', help='specify a recipient to be ' 'blindly copied on the e-mail') parser.add_option('--subject', dest='subject', help='specify a subject for the e-mail') parser.add_option('--body', dest='body', help='specify a body for the ' 'e-mail. Since the user will be able to make changes ' 'before actually sending the e-mail, this can be used ' 'to provide the user with a template for the e-mail ' 'text may contain linebreaks') parser.add_option('--attach', dest='attach', help='specify an attachment ' 'for the e-mail. file must point to an existing file') (options, args) = parser.parse_args() if not args: parser.print_usage() parser.exit(1) if options.mailto_mode: if not mailto(args, None, options.cc, options.bcc, options.subject, options.body, options.attach): sys.exit('Unable to open the e-mail client') else: for name in ('cc', 'bcc', 'subject', 'body', 'attach'): if getattr(options, name): parser.error('The "cc", "bcc", "subject", "body" and "attach" ' 'options are only accepten in mailto mode') success = False for arg in args: if not open(arg): print('Unable to open "%s"' % arg) else: success = True sys.exit(success)
python calling apis events and printing randomly
I have a interval of 60 and wanted to print 6 events every 1 minutes. But it prints 11,12 and 13 events randomly every 1 minutes. Why is that so ? Is it because of my codes or what other factors can cause this ? My code is - import logging import httplib import simplejson as json import socket import time import datetime import urllib2 import sys import xml.dom.minidom from bs4 import BeautifulSoup as soup SCHEME = """<scheme> <title>testingCurrentWeatherSG</title> <description>Get data from forecast.</description> <use_external_validation>true</use_external_validation> <streaming_mode>simple</streaming_mode> <endpoint> <args> <arg name="intervalone"> <title>Intervalone</title> <description>How long to refresh this query?</description> </arg> </args> </endpoint> </scheme> """ def do_scheme(): print SCHEME ## Utility functions def fahrenheit(fahren): return (fahren-32) * 5.0/9.0 def get_percent(num): return num * 100. ## Responses def get_response(conn, url): try: conn.request('GET', url) result = conn.getresponse() data = result.read() return json.loads(data) except socket.timeout: return None ## Printing def print_forecast(name, di): # Print the forcast from 'di', for location 'name' # name is the name of the location, di is the api response psi_avg=20 current = di['currently'] for key, value in sorted(current.iteritems()): if key in ['cloudCover', 'icon', 'ozone', 'precipIntensity', # time 'precipProbability', 'precipType', 'pressure', 'summary', 'visibility', 'windBearing', 'windSpeed']: print '{0} : {1}'.format(key, value) elif key in ['temperature', 'dewPoint']: print '%s: %.2f' % (key, fahrenheit(value)) elif key == 'humidity': print '%s: %.2f' % (key, get_percent(value)) print 'psiAverage : ' + str(psi_avg) print 'latitude : ' + str(di['latitude']) print 'longitude : ' + str(di['longitude']) print 'location : ' + str(name) print def weather_Connection(intervalone): host = 'api.forecast.io' conn = httplib.HTTPSConnection(host, timeout=60) # adjust timeout as desired try: urlnyp = '/forecast/59ff8cb7661d231f2967c2663c0a3bdc/1.37871,103.848808' conn.request('GET', urlnyp) resultnyp = conn.getresponse() contentnyp = resultnyp.read() except socket.timeout: print 'socket timeout' return # the locations and urls for the api calls urls = { 'Choa Chu Kang': '/forecast/59ff8cb7661d231f2967c2663c0a3bdc/1.394557,103.746396', 'Kallang': '/forecast/59ff8cb7661d231f2967c2663c0a3bdc/1.311469,103.871399', 'Jurong West': '/forecast/59ff8cb7661d231f2967c2663c0a3bdc/1.352008,103.698599', 'Redhill': '/forecast/59ff8cb7661d231f2967c2663c0a3bdc/1.289732,103.81675', 'Tampines': '/forecast/59ff8cb7661d231f2967c2663c0a3bdc/1.353092,103.945229', 'Yishun': '/forecast/59ff8cb7661d231f2967c2663c0a3bdc/1.429463,103.84022', } responses = {} for i, (name, url) in enumerate(sorted(urls.iteritems())): response = get_response(conn, url) if not response: print 'socket timeout on url#%d: %s' % (i, url) return responses[name] = response conn.close() # print the forecast for name, data in responses.iteritems(): print_forecast(name, data) def get_config(): #Read XML Configuration data passed from splunkd on stdin config = {} try: # read everything from stdin config_str = sys.stdin.read() # parse the config XML doc = xml.dom.minidom.parseString(config_str) root = doc.documentElement conf_node = root.getElementsByTagName("configuration")[0] if conf_node: logging.debug("XML: found configuration") stanza = conf_node.getElementsByTagName("stanza")[0] if stanza: stanza_name = stanza.getAttribute("name") if stanza_name: logging.debug("XML: found stanza " + stanza_name) config["name"] = stanza_name params = stanza.getElementsByTagName("param") for param in params: param_name = param.getAttribute("name") logging.debug("XML: found param '%s'" % param_name) if param_name and param.firstChild and \ param.firstChild.nodeType == param.firstChild.TEXT_NODE: data = param.firstChild.data config[param_name] = data logging.debug("XML: '%s' -> '%s'" % (param_name, data)) if not config: raise Exception, "Invalid configuration received from Splunk." except Exception, e: raise Exception, "Error getting Splunk configuration via STDIN: %s" % str(e) return config def run(): #The Main function that starts the action. The thread will sleep for however many seconds are configured via the Input. # config = get_config() # # # intervalone = config["intervalone"] intervalone =60 while True: weather_Connection(intervalone) logging.info("Sleeping for %s seconds" %(intervalone)) time.sleep(float(intervalone)) if __name__ == '__main__': if len(sys.argv) > 1: if sys.argv[1] == "--scheme": do_scheme() else: run() sys.exit(0)
I've checked and tried your code and it works fine. Try replacing logging.info("Sleeping for %s seconds" %(intervalone)) with print("Sleeping for %s seconds" % (intervalone)) You should see this statement every 6 forecasts. Note: why returning from weather_Connection() here for i, (name, url) in enumerate(sorted(urls.iteritems())): response = get_response(conn, url) if not response: print 'socket timeout on url#%d: %s' % (i, url) return responses[name] = response You can just skip it with continue for i, (name, url) in enumerate(sorted(urls.iteritems())): response = get_response(conn, url) if not response: print 'socket timeout on url#%d: %s' % (i, url) continue responses[name] = response