have the following code:
import os
import sys
import time
while True:
#type, timeLogged,timeQueued,orig,rcpt,orcpt,dsnAction,dsnStatus,dsnDiag,dsnMta,bounceCat,srcType,srcMta,dlvType,dlvSourceIp,dlvDestinationIp,dlvEsmtpAvailable,dlvSize,vmta,jobId,envId,queue,vmtaPool
#Example-: d,5/13/2014 13:57,5/13/2014 13:57,Sandals#origindomain.com,user#gmail.com,,relayed,2.0.0 (success),smtp;250 2.0.0 OK 1400003914 yu4si17175723obb.54 - gsmtp,gmail-smtp-in.l.google.com (173.194.64.26),,smtp,[192.168.100.1] (208.115.206.226),smtp,208.115.206.228,173.194.64.26,ENHANCEDSTATUSCODES,CHUNKING,8BITMIME,SIZE,STARTTLS,8405,mx3.yeahdeal.com,28,,gmail.com/mx3.orgindomain.com,orgindomain.com
line = sys.stdin.readline()
logList = line.split(',')
bounceType = str(logList[0])
if bounceType != "type":
bounceType = str(logList[0])
bounceCategory = logList[10]
emailAddress = logList[4]
jobId = logList[19]
fwrite = open("debug.log","a")
fwrite.write(str(jobId))
fwrite = open("debug2.log","w")
fwrite.write("out of true loop")
dbcon.close()
When I check debug.log, jobId is "SIZE" (it should be a number--have also tried without STR, and tried with int()). Some of the other variables in my logList[] actually are the value that I want them to be. Very new to python, so I'm guessing I'm doing something wrong.
Related
I'm writing a script to sent proxies file from a directory to telegram channel. With the help of some good folks I was able to complete it almost
from telegram import Bot, InputMediaDocument
BOT_TOKEN = "xxx"
CHAT_ID = xxx
def main():
bot = Bot(BOT_TOKEN)
file_paths = (
"proxy/proxies/http.txt",
"proxy/proxies/socks4.txt",
"proxy/proxies/socks5.txt"
)
path_http = "proxy/proxies/http.txt"
with open(path_http,'rb') as file_http:
http_count = len(file_http.readlines())
file_http.close()
path_socks4 = 'proxy/proxies/socks4.txt'
with open(path_socks4,'rb') as file_socks4:
socks4_count = len(file_socks4.readlines())
file_socks4.close()
path_socks5 = 'proxy/proxies/socks5.txt'
with open(path_socks5,'rb') as file_socks5:
socks5_count = len(file_socks5.readlines())
file_socks5.close()
text = f"Total HTTPs:{http_count}\nTotal SOCKS4:{socks4_count}\nTotal SOCKS5:{socks5_count}"
media_group = list()
for f in file_paths:
with open(f, "rb") as fin:
caption = text if f == "proxy/proxies/http.txt" else ''
fin.seek(0)
media_group.append(InputMediaDocument(fin, caption=caption))
bot.send_media_group(CHAT_ID, media=media_group)
if __name__ == "__main__":
main()
Issues occurred
media_group = list()
^
IndentationError: unindent does not match any outer indentation level
what I'm trying is to send those 3 proxy files with a caption to the first proxy file
There are a few issues with your code:
The lack of proper indentation
Improper closing of brackets
The SyntaxError that you're getting on the line path_http = 'proxy/proxies/http.txt' is because you didn't close the brackets from the previous line (the line where you initialised the variable file_paths).
I also foresee that you'll face issues when you try to open the file with the open function. You can look up some examples here: https://www.programiz.com/python-programming/file-operation
with the help of some good folks I make it work, If there are any easy versions fell free to share
from telegram import Bot, InputMediaDocument
BOT_TOKEN = "xxxxx"
CHAT_ID = -1000000
def http():
path_http = 'proxy/proxies/http.txt'
with open(path_http, 'rb') as file_http:
http_count = len(file_http.readlines())
return http_count
def socks4():
path_socks4 = 'proxy/proxies/socks4.txt'
with open(path_socks4, 'rb') as file_socks4:
socks4_count = len(file_socks4.readlines())
return socks4_count
def socks5():
path_socks5 = 'proxy/proxies/socks5.txt'
with open(path_socks5, 'rb') as file_socks5:
socks5_count = len(file_socks5.readlines())
return socks5_count
http_count = http()
socks4_count = socks4()
socks5_count = socks5()
text = f"Total HTTPs:{http_count}\nTotal SOCKS4:{socks4_count}\nTotal SOCKS5: {socks5_count}"
def main():
bot = Bot(BOT_TOKEN)
file_paths = (
"proxy/proxies/http.txt",
"proxy/proxies/socks4.txt",
"proxy/proxies/socks5.txt"
)
media_group = list()
for f in file_paths:
with open(f, "rb") as fin:
caption = text if f == "proxy/proxies/socks5.txt" else ''
fin.seek(0)
media_group.append(InputMediaDocument(fin, caption=caption))
bot.send_media_group(CHAT_ID, media=media_group)
if __name__ == "__main__":
main()
`file_paths = (
"proxy/proxies/http.txt",
"proxy/proxies/socks4.txt",
"proxy/proxies/socks5.txt"
`
invalid syntax error probably because ) doesn't exist in the end.
try adding (" ") caption = text if f == "proxy/proxies/http.txt" else '' because it is a string.
I have a table that contains a few categories and two of them are: mac address and device name. I had a the list of my mac address written in my code (hardcoded) with their corresponding device names (ie deviceDict['00:00:00:00:00:00']= name)
Now, I passed those mac addresses and device names to a text file to be read from that same Python code and parse it onto my table. The code currently recognizes the text file but it is not parsing that information onto the table.
Here is the code:
# File: WapLogParser.py
# Desc: Parses a WAP log file and pulls out information relating to connected clients
# Usage: python WapLogParser.py [file glob]
import re
import sys
import glob
import os
deviceDict = dict()
# Base table for storing client info
# All names must match what is in the Wap Log file
# Exceptions: Date, Wap Name, Device Name - which are provided outside of the result parsing
table = [["Ssid", "Vlan", "Mac Address", "Connected Time", "Ip Address", "Rssi", "Date", "Wap Name", "Device Name"]]
def ParseResult(result, date, wapName):
lines = result.split('\n')
lines = list(filter(None, lines))
# Any useful info will be at least 2 lines long
if len(lines) == 1:
return
# create empty row
data = [""] * len(table[0])
# for each item in the result place it in the correct spot in the row
for line in lines:
if line != "":
# Parse the key/value pair
m = re.match(r"(.*):\s\.*\s?(.*)", line)
if m is not None:
for idx in range(len(table[0])):
if table[0][idx].lower() == m[1].lower():
data[idx] = m[2]
else:
break
# Remove the '(dBm)' from the RSSI value
data[5] = data[5].split()[0]
# Append WAP specific items to row
data[6] = date
data[7] = wapName
data[8] = GetDeviceName(data[2].upper())
# Add row to table
table.append(data)
def ParseFile(path):
with open(path) as f:
lines = f.readlines()
result = ""
command = ""
date = ""
# WAP name is always on the first line 16 characters in with 4
# unnecessary characters trailing
wapName = lines[0].strip()[16:-4]
for line in lines:
line = line.strip()
# Is an issued command?
if line.startswith("/#"):
if command != "":
ParseResult(result, date, wapName)
command = ""
# reset the result for the new command
result = ""
m = re.match(r"^/#.*show\sclient.*stats$", line)
if m is not None:
command = line
# Anything that is not a command add to the result
else:
result += line + "\n"
# Do we have the date?
if line.startswith("Current date:"):
date = line.replace("Current date: ", "")
# Print output to stderr
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
# Print a 2d array in a csv format
def PrintAsCsv(table):
for row in table:
print(",".join(row))
def Main():
InitDeviceDict()
numArgs = len(sys.argv)
for filename in glob.iglob(sys.argv[numArgs - 1], recursive=True):
# Globs get directories too
if os.path.isfile(filename):
eprint("Parsing " + filename)
try:
ParseFile(filename)
except Exception as e: # Mainly for if we see a binary file
eprint("Bad file: " + e)
# Print in a format we can use
PrintAsCsv(table)
def GetDeviceName(macAddress):
if macAddress in deviceDict:
return deviceDict[macAddress]
manufacturerPart = macAddress[:8]
if manufacturerPart in deviceDict:
return deviceDict[manufacturerPart]
return 'Unknown Device'
def InitDeviceDict():
with open('try.txt','r') as fo:
for line in fo:
deviceDict = {}
line = line.split(',')
macAddress = line[0].strip()
manufacturerPart = line[1].strip()
if macAddress in deviceDict:
deviceDict[macAddress].append(manufacturerPart)
else:
deviceDict[macAddress]=(manufacturerPart)
print(deviceDict)
# entry point
# script arguments:
# WapLogParser.py [file glob]
if __name__ == "__main__":
Main()
The issue is on the functions GetDeviceName and InitDeviceDict. When I run the code and then a batch file to display my info on excel, I keep getting "unknown device" (as if it is not recognizing the mac address I entered to produce the device name)
Any way I can correct this? Thank you
The deviceDict that is populated in InitDeviceDict is not the global deviceDict. You are only modifying a function-local dictionary (and resetting it every line as well). Remove deviceDict = {} from that function and, at the top of the function use global deviceDict to declare that you are modifying the global.
def InitDeviceDict():
global deviceDict
with open('try.txt','r') as fo:
for line in fo:
line = line.split(',')
macAddress = line[0].strip()
manufacturerPart = line[1].strip()
if macAddress in deviceDict:
deviceDict[macAddress].append(manufacturerPart)
else:
deviceDict[macAddress]=[manufacturerPart]
I would like to redirect the print statements of my output to a text file. I have imported the sys and tried below.
import pprint
import os
import math
import sys
class ExportLimits(object):
MMAP_ITER_TRIPS = 'manpower_mappings.map_iterator_trips'
def __init__(self, workset, crew_type, bid_period, divisor_files=None):
log_file = "/opt/test/test_user/data/ESR_DATA/etab/slogfile.txt"
sys.stdout = open(log_file, 'w')
self.workset = workset
self.crew_type = crew_type
self.bid_period = bid_period
self.tm = workset.getTM()
self.crew_bid_period = self.crew_type + "+" + self.bid_period
self.bid_period = self.tm.table('qf_user_bid_period')[(self.crew_bid_period)]
self.period = Period(self.bid_period.bpstart, self.bid_period.bpend)
self.filter_matcher = self._get_filter_matcher()
self.iterator_trips = rave_api.eval(\
ExportLimits.MMAP_ITER_TRIPS)[0]
self.divisor_reader_lh = divisor_reader_lh.DivisorReader(\
divisor_files=divisor_files)
self.divisor_reader_sh = divisor_reader_sh.DivisorReader(\
divisor_files=divisor_files)
self.pp_start = self.period.getStart()
self.pp_end = self.period.getEnd()
def export_limits(self, item_type):
if item_type == 'DKSH':
self._mandays_limits(SLKHH_GROUPS)
else:
self._mandays_limits(LAJSDLH_GROUPS)
def _mandays_limits(self, groups):
crews = [self.tm.table('crew')[('99172447',)],
self.tm.table('crew')[('7654678',)]]
generator = ((crew, self.filter_matcher.getFilterNamePeriodsMap(crew.id))
for crew in self.tm.table('crew'))
minres = defaultdict(lambda :RelTime(0))
maxres = defaultdict(lambda :RelTime(0))
for crew, group_to_periods in generator:
print crew, group_to_periods
try:
crew_filter, period = group_to_periods.iteritems().next()
except StopIteration:
continue
if crew_filter not in groups:
continue
It works partially for me. I am able to print few of the lines, but not completely. Please find the below output of my log file where it has only printed fewer lines but not the complete logs.
For some reason, it hasn't printed completely. (Please see the last line of the log file where it printed only till "alia".)
Log File:
crew _id="133245" id="176543" empno="8761890" sex="M"
birthday="19681217" name="MICHEAL" forenames="LUCAS" maincat="C"
preferredname="ESWAR" initials="LL" joindate="20010910 00:00"
comjoindate="20010910 00:00"
_void="title,logname,si,bcity,bstate,bcountry,alias,comenddate" {'X-SYD-BB-AUSLLH': [26JUN2017 00:00-21AUG2017 00:00]}
crew _id="214141" id="132451" empno="145432" sex="M"
birthday="19630904" name="ESWARF" forenames="FJDJSK" maincat="C"
preferredname="ESWADF" initials="WL" joindate="20010910 00:00"
comjoindate="20010910 00:00"
_void="title,logname,si,bcity,bstate,bcountry,alia
~
~
Please check and advise.
Instead of using sys.stdout you can write like:
output_file = open(log_file, 'w')
output_file.write('testing asdasdfsd')
Or if you want to write all kinds of print value in log file then :
output_file = open(log_file, 'w')
sys.stdout = output_file
that's it.
Case: I have a python file which upon execution generates a output as integer which I want to write it in another file and load the generated file to another python script to execute further.
what I have tried till now:
py1:
import os
import json
print('Exchanges')
url_stc = os.popen("curl -u *****:*****
https://api.exchanges.com/count").read()
data_stc = json.loads(url_stc)
if (p_count > 3):
stc_n = p_count
print('Stock counts are increased to:')
print stc_n
else:
stc_n = 3
print stc_n
print('rates')
url_rates = os.popen("curl -u *****:*****
https://api.exchanges.com/rates").read()
data_rates = json.loads(url_rates)
if (p_rates > 1):
rates_n = p_rates
print('rate counts are increased to:')
print rates_n
else:
rates_n = 1
print rates_n
output: stc_n, rates_n
I want the output which will be a integer to be written in a file every time I execute the code a (new file) should be generated and also old file should be replaced with the new one.
py2:
from new file import *
def abc():
if(stc_n == 3):
## do some stuff
def mnq():
if(rates_n == 1):
##do some stuff
I am trying the same with
f = file("/new_file.txt",'r')
data = f.read()
if not len(data) > 4:
variable1 = "3" # note this data is what to be returned from new file
variable2 = "2" # note this data is what to be returned from new file
else:
data = string.split(data,"\n")
variable1 = int(data[0])
variable2 = int(data[1])
What I am missing here any clues?
For some unknown reason, when I run the below script, the following error is returned along with the desired output. For some reason, this was working without any errors last night. The API output does change every minute but I wouldn't expect a KeyError to be returned. I can't simply pinpoint where this error is coming from:
[u'#AAPL 151204C00128000'] <----- What I want to see printed
Traceback (most recent call last):
File "Options_testing.py", line 60, in <module>
main()
File "Options_testing.py", line 56, in main
if quotes[x]['greeks']['impvol'] > 0: #change this for different greek vals
KeyError: 'impvol'
Here is a little snippet of data:
{"results":{"optionchain":{"expire":"all","excode":"oprac","equityinfo":{"longname":"Apple Inc","shortname":"AAPL"},"money":"at","callput":"all","key":{"symbol":["AAPL"],"exLgName":"Nasdaq Global Select","exShName":"NGS","exchange":"NGS"},"symbolstring":"AAPL"},"quote":[{"delaymin":15,"contract":{"strike":108,"openinterest":3516,"contracthigh":6.16,"contractlow":0.02,"callput":"Put","type":"WEEK","expirydate":"2015-11-13"},"root":{"equityinfo":{"longname":"Apple Inc","shortname":"AAPL"},"key":{"symbol":["AAPL"],"exLgName":"Nasdaq Global Select","exShName":"NGS","exchange":"NGS"}},"greeks":{"vega":0,"theta":0,"gamma":0,"delta":0,"impvol":0,"rho":0}
Code:
#Options screener using Quotemedia's API
import json
import requests
#import csv
def main():
url_auth= "https://app.quotemedia.com/user/g/authenticate/v0/102368/XXXXX/XXXXX"
decode_auth = requests.get(url_auth)
#print decode_auth.json()
#print(type(decode_auth))
auth_data = json.dumps(decode_auth.json())
#Parse decode_auth, grab 'sid'
sid_parsed = json.loads(auth_data)["sid"]
#print sid_parsed
#Pass sid into qm_options
#Construct URL
symbol = 'AAPL'
SID = sid_parsed
url_raw = 'http://app.quotemedia.com/data/getOptionQuotes.json?webmasterId=102368'
url_data = url_raw + '&symbol=' + symbol + '&greeks=true' + '&SID=' + SID
#print url_data
response = requests.get(url_data)
#print response
data = json.dumps(response.json())
#print data
#save data to a file
with open('AAPL_20151118.json', 'w') as outfile:
json.dumps (data, outfile)
#Turn into json object
obj = json.loads(data)
#slim the object
quotes = obj['results']['quote']
#find the number of options contracts
range_count = obj['results']['symbolcount']
#print all contracts with an implied vol > 0
for x in range(0,range_count):
if quotes[x]['greeks']['impvol'] > 0: #change this for different greek vals
print quotes[x]['key']['symbol']
if __name__ == '__main__':
main()
I can provide sample data if necessary.
for x in range(0,range_count):
if quotes[x]['greeks']['impvol'] > 0: #change this for different greek vals
print quotes[x]['key']['symbol']
This loops throug multiple quotes, so maybe there is even just one that does not have an impvol property.
You should add some error handling, so you find out when that happens. Something like this:
# no need to iterate over indexes, just iterate over the items
for quote in quotes:
if 'greeks' not in quote:
print('Quote does not contain `greeks`:', quote)
elif 'impvol' not in quote['greeks']:
print('Quote does not contain `impvol`:', quote)
elif quote['greeks']['impvol'] > 0:
print quote['key']['symbol']