This Question is Python Flask, Turbo-Flask related.
I've recently faced a strange error with it and I would like to ask for some help from you if it's okay.
I am pushing the update with the below codes, and it is within a loop, it works most of the time wonderfully, But randomly (after few cycle run), I get the below error.
# content push
with app.app_context():
turbo.push(turbo.replace(render_template('base.html',
now= datetime.utcnow().strftime("%d-%b-%y %H:%M:%S"),
timestamp_inject = timestamp,
in_k_inject= in_k,
out_k_inject= out_k,
input_error_inject= input_error),
'update_utilpoll'))
This is the error message I get.
Exception in thread Thread-7:
Traceback (most recent call last):
File "C:\Users\jsb\Anaconda3\lib\threading.py", line 973, in _bootstrap_inner
self.run()
File "C:\Users\jsb\Anaconda3\lib\threading.py", line 910, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\jsb\my_bot\FLASK\Flask-chart\app.py", line 179, in util_pulling
utilpoll (connection, device)
File "C:\Users\jsb\my_bot\FLASK\Flask-chart\app.py", line 152, in utilpoll
turbo.push(turbo.replace(render_template('base.html',
File "C:\Users\jsb\Anaconda3\lib\site-packages\turbo_flask\turbo.py", line 197, in push
for recipient in to:
RuntimeError: dictionary changed size during iteration
The variable that I am sending for tendering is 'List' variable.
Related
I'm trying to build some code to restart. Here is the problem. When I restart the app from source, the restart works fine. But, when I compile it, the code restart doesn't work properly. The following error appears:
usage: MyApp [-h] [-c CONFIG] [-C CACHE] [-l LOG] [--devices] [--default-config]
MyApp: error: unrecognized arguments: /home/myapp/MyApp
But I'm not passing any args. In this case, the restart code is passing the path of the program which is, it shouldn't.
If I start the app, its starting fine. It's only on restart that there's a problem.
I'm using debian11, Python 3.9.2. And pyinstaller 5.7.0.
Here is my restart code.
class RestartCommand(Command):
#property
def help(self) -> str:
return "Restarts the app"
def __call__(self, arg: str, user: User) -> Optional[str]:
self._myapp.close()
try:
if 'frozen' in sys.builtin_module_names:
executable = sys.executable
if sys.platform == "win32":
subprocess.run([executable] + sys.argv[1:])
else:
subprocess.run([executable, "-m", "__main__"] + sys.argv[1:])
else:
args = sys.argv
if sys.platform == "win32":
subprocess.run([sys.executable] + args)
else:
args.insert(0, sys.executable)
os.execv(sys.executable, args)
except Exception as e:
print("Error while restarting: ", e)
I try to remove the argv like this.
if 'frozen' in sys.builtin_module_names:
executable = sys.executable
if sys.platform == "win32":
subprocess.run([executable])
else:
subprocess.run([executable, "-m", "__main__"])
But, the same error still appears.
I try to read to documentation but found nothing.
import commands
import tempfile
cluster = commands.getoutput('lsid | grep "My cluster name" |awk \'{print $5}\'')
path = '/nxdi_env/lsf/conf/lsbatch/' + cluster + '/configdir/lsb.users'
bl = commands.getoutput('sed -n \'/# BL Fairshare group/,/(it_normal it_priority)/p\' %s | grep -v ^# | sed \'/^$/d\'' %path)
grp = ''
group = ''
txt = ''
normal_detail = ''
priority_detail = ''
subgrp_detail = ''
def print_group(g,sub):
global normal_detail
global priority_detail
subgrp = (group + '_' + sub).lower()
subgrp1 = subgrp + ','
if sub == 'normal':
subgrp_share = txt[txt.find(subgrp1)+len(subgrp1):txt.find("]")]
subgrp_detail = normal_detail
else:
subgrp_share = txt[txt.find(subgrp1)+len(subgrp1):txt.find("])")]
subgrp_detail = priority_detail
subgrp_detail = subgrp_detail.strip().split('(all) ')
subgrp_detail = subgrp_detail[1].replace('
','').replace('([','').replace('])','').split('][')
print(' |- %s \t %-5s' %(subgrp,subgrp_share))
for i in subgrp_detail:
user, slot = i.split(',')
print(' | | - %-13s%-5s ' %(user,slot))
with tempfile.NamedTemporaryFile(delete=True) as f:
f.write(bl)
f.seek(0)
for line in f:
grp = line.split()
if '_' in grp[0]:
subgrp = grp[0]
group = subgrp.split('_')
group = group[0].upper()
if 'normal' in grp[0]:
normal_detail = line
else:
priority_detail = line
else:
print(group)
txt = line
print_group(group, 'normal')
print_group(group, 'priority')
I am trying to make it so that i can connect to this server from other networks around the world, as you see on the screenshot provided i have port-forwarding set up to forward all request on external port 6000 to my static ip address stored in the host variable.
#This is my Server
import socket
import threading
host = '192.168.1.135'
port = 6000
#s = socket.socket()
s = socket.socket()#socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen()
def send():
mess = input("--> ")
con.send(bytes(mess, 'utf-8'))
def receive():
msg = con.recv(1024)
print(msg.decode('utf-8'))
while True:
try:
con, address = s.accept()
print(f'Connection to {address} made successfully!')
receive()
send()
except:
pass
This is my client file below, i took out my public ip address from the host variable for obvious reasons... When i try connecting while connected to the network the server is running on it joins fine, in the server file where it prints the ip address it shows my networks gateway address as the connected ip. But when when trying to connect from outside the network it doesn't work!
import socket
host = 'public ip'; port = 6000
def send():
mess = input('--> ')
s.send(bytes(mess,"utf-8"))
def receive():
msg = s.recv(1024)
print(msg.decode('utf-8'))
while True:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((host, port))
send()
receive()
except:
pass
This link will show you a picture of the settings i have set in my network settings.
https://i.stack.imgur.com/TmNYc.png
I am pretty new to the whole "port-forwarding" idea but cannot seem to find the issue after over a week of trying hundreds of solutions nothing seems to work... If you need any more information to help in solving this just ask, thank you!
(i cant add a comment yet)
first try to set the server ip to '0.0.0.0' and try to use the port 6000 on the server and client and dont use diffrent port and forward diffrent one
I am trying to send an email using a python program which i have attached with this.In this i am using smtp server, the code is working fine when i try with my mobile data connection but fails when i try in my college wifi network.Even I have tried all the following port numbers 25,465,587,2525 & 2526 , but still its not working.So suggest a way to send email in such a network or some other related sources in python or C++.
# Python 3
import smtplib
port_number = 587 # 25
def send_Mail(src,des,password,message):
global port_number
try:
print("Start")
server = smtplib.SMTP('smtp.gmail.com',port_number) # Failing in this line
print("Connection Established")
server.ehlo()
print("Extended hello done")
server.starttls()
print("tls Handshake completed")
server.login(src,password)
print("Logged in")
server.sendmail(src,des,message)
print("Mail sent")
server.quit()
print("Success!")
except:
pass
mail_of_sender = "example#example.com"
recipient_mail_id = "test#example.com"
password = "testingpassword"
message = "Testing"
send_Mail(mail_of_sender,recipient_mail_id,password,message)
I have created a script which fetches geocodes(latitudes and longtitudes) of a sample of addresses
However I want it to also return the accuracy/precision of these geocodes that are mined. for example: 100% accurate or 90% etc
# Set the input and output files
input_file_path = "geocodi_4k-5250.csv"
output_file_path = "output" # appends "####.csv" to the file name when it writes the file.
# Set the name of the column indexes here so that pandas can read the CSV file
address_column_name = "ADDRESS"
state_column_name = "STATE"
zip_column_name = "ZIP_CODE" # Leave blank("") if you do not have zip codes
# Where the program starts processing the addresses in the input file
# This is useful in case the computer crashes so you can resume the program where it left off or so you can run multiple
# instances of the program starting at different spots in the input file
start_index = 0
# How often the program prints the status of the running program
status_rate = 100
# How often the program saves a backup file
write_data_rate = 1000
# How many times the program tries to geocode an address before it gives up
attempts_to_geocode = 3
# Time it delays each time it does not find an address
# Note that this is added to itself each time it fails so it should not be set to a large number
wait_time = 3
# ----------------------------- Processing the input file -----------------------------#
df = pd.read_csv(input_file_path, low_memory=False)
# df = pd.read_excel(input_file_path)
# Raise errors if the provided column names could not be found in the input file
if address_column_name not in df.columns:
raise ValueError("Can't find the address column in the input file.")
if state_column_name not in df.columns:
raise ValueError("Can't find the state column in the input file.")
# Zip code is not needed but helps provide more accurate locations
if (zip_column_name):
if zip_column_name not in df.columns:
raise ValueError("Can't find the zip code column in the input file.")
addresses = (df[address_column_name] + ', ' + df[zip_column_name].astype(str) + ', ' + df[state_column_name]).tolist()
else:
addresses = (df[address_column_name] + ', ' + df[state_column_name]).tolist()
# ----------------------------- Function Definitions -----------------------------#
# Creates request sessions for geocoding
class GeoSessions:
def __init__(self):
self.Arcgis = requests.Session()
self.Komoot = requests.Session()
# Class that is used to return 3 new sessions for each geocoding source
def create_sessions():
return GeoSessions()
# Main geocoding function that uses the geocoding package to covert addresses into lat, longs
def geocode_address(address, s):
g = geocoder.arcgis(address, session=s.Arcgis)
if (g.ok == False):
g = geocoder.komoot(address, session=s.Komoot)
return g
def try_address(address, s, attempts_remaining, wait_time):
g = geocode_address(address, s)
if (g.ok == False):
time.sleep(wait_time)
s = create_sessions() # It is not very likely that we can't find an address so we create new sessions and wait
if (attempts_remaining > 0):
try_address(address, s, attempts_remaining-1, wait_time+wait_time)
return g
# Function used to write data to the output file
def write_data(data, index):
file_name = (output_file_path + str(index) + ".csv")
print("Created the file: " + file_name)
done = pd.DataFrame(data)
done.columns = ['Address', 'Lat', 'Long', 'Provider']
done.to_csv((file_name + ".csv"), sep=',', encoding='utf8')
# Variables used in the main for loop that do not need to be modified by the user
s = create_sessions()
results = []
failed = 0
total_failed = 0
progress = len(addresses) - start_index
# ----------------------------- Main Loop -----------------------------#
for i, address in enumerate(addresses[start_index:]):
# Print the status of how many addresses have be processed so far and how many of the failed.
if ((start_index + i) % status_rate == 0):
total_failed += failed
print(
"Completed {} of {}. Failed {} for this section and {} in total.".format(i + start_index, progress, failed,
total_failed))
failed = 0
# Try geocoding the addresses
try:
g = try_address(address, s, attempts_to_geocode, wait_time)
if (g.ok == False):
results.append([address, "was", "not", "geocoded"])
print("Gave up on address: " + address)
failed += 1
else:
results.append([address, g.latlng[0], g.latlng[1], g.provider])
# If we failed with an error like a timeout we will try the address again after we wait 5 secs
except Exception as e:
print("Failed with error {} on address {}. Will try again.".format(e, address))
try:
time.sleep(5)
s = create_sessions()
g = geocode_address(address, s)
if (g.ok == False):
print("Did not fine it.")
results.append([address, "was", "not", "geocoded"])
failed += 1
else:
print("Successfully found it.")
results.append([address, g.latlng[0], g.latlng[1], g.provider])
except Exception as e:
print("Failed with error {} on address {} again.".format(e, address))
failed += 1
results.append([address, e, e, "ERROR"])
# Writing what has been processed so far to an output file
if (i%write_data_rate == 0 and i != 0):
write_data(results, i + start_index)
# print(i, g.latlng, g.provider)
# Finished
write_data(results, i + start_index + 1)
print("Finished! :)")
My input file looks like:
ADDRESS STATE ZIP_CODE
21236 Birchwood Loop AK 99567
1731 Bragaw St AK 99508
300 E Fireweed Ln AK 99503
Output is:
Address Lat Long
21236 Birchwood Loop, 99567, AK 61.40886875 -149.4865564
1731 Bragaw St, 99508, AK 61.20489474 -149.808293
300 E Fireweed Ln, 99503, AK 61.1980295 -149.8783492
I want it to also show how accurate the geocodes are.How do I do the same
I don't have a clear answer on this. However, some solutions come to mind:
Test your geocoding against a standard list (a list with addresses with their correct geocodes). This will tell you how your geocoding performs generally.
Try multiple geocoding services. If you have that the geocodes match across services, there is a high chance that the geocode is correct. One thing to be careful about is that you may not have exactly correct matches, so you may have to calculate the distance between the points.
Did you find any other solution to this problem. I'm working on something similar and would like to hear if you have other ideas.