I have a script that generates a CSV by calling an API lots of times that has successfully run in the past, however now I am getting Errno messages. Most commonly [Errno 54] Connection reset by peer, [Errno 60] Operation timed out, and sometimes [Errno 8] nodename nor servname provided, or not known.
This specific script calls an API around 16-24k times (I am 100% sure this is below the ratelimit) and when it ran successfully took around 2-3 hours to complete. For some reason, I can't seem to get it to run successfully without getting an error of some sort a while into the script running. So it will call an API route hundreds/thousands of time and then out of nowhere spit out one of the errors for calling the same exact one it's already been calling successfully. It seems strange. I've provided the error messages below as well.
I've removed some sensitive information such as the token and API routes as this is private data! I've left the errors in tact though.
import http.client
import json
import csv
class mgrData:
def __init__(self):
self.token = '[REMOVED]'
self.userDict = {}
def getPeakData(self, route):
conn = http.client.HTTPSConnection("[REMOVED]")
bod = ''
headers = {
'Authorization': 'Bearer '+ str(self.token),
'Content-Type': 'application/json'
}
conn.request("GET", route, bod, headers)
res = conn.getresponse()
data = res.read()
return json.loads(data)
def PeakExport(self):
print("Please wait, data generating...")
raw = self.getPeakData("[REMOVED]")
totalrecords = raw['meta']['page']['total']
resultspp = len(raw['data'])
if totalrecords % resultspp == 0:
numpages = int(totalrecords/resultspp)
elif totalrecords % resultspp != 0:
numpages = int(totalrecords//resultspp + 1)
mainDct = {}
for i in range(1,numpages+1):
route = "[REMOVED]"
dct = self.getPeakData(route)
data = dct['data']
for i in data:
pkEmpID = i['attributes']['identifier'].split('_')[0]
name = i['attributes']['name']
# segID = i['id']
hrisIDget = self.getPeakData("[REMOVED]")
hrisID = hrisIDget['urn:ietf:params:scim:schemas:extension:enterprise:2.0:User']['employeeNumber']
if pkEmpID in mainDct:
if i['attributes']['direct'] == True:
dSegID = i['id']
mainDct[pkEmpID].append({'dSegID' : dSegID})
if i['attributes']['direct'] == False:
aSegID = i['id']
mainDct[pkEmpID].append({'aSegID' : aSegID})
else:
mainDct.update({pkEmpID : []})
mainDct[pkEmpID].append({'name': name})
mainDct[pkEmpID].append({'EID' : hrisID})
if i['attributes']['direct'] == True:
dSegID = i['id']
mainDct[pkEmpID].append({'dSegID' : dSegID})
if i['attributes']['direct'] == False:
aSegID = i['id']
mainDct[pkEmpID].append({'aSegID' : aSegID})
urllist = []
for key in mainDct:
switch = False
for i in mainDct[key]:
if 'aSegID' in i:
switch = True
data = self.getPeakData("[REMOVED]" % (i['aSegID']))
if 'mean' in data['data']['attributes']['scores']:
score = data['data']['attributes']['scores']['mean']
participation = data['data']['attributes']['participation']['mean']
nps = data['data']['attributes']['scores']['nps']['score']
mainDct[key].append({'engagement all': score})
mainDct[key].append({'nps all': nps})
mainDct[key].append({'participation all' : participation})
elif 'anonymity' in data['data']['attributes']['scores']:
mainDct[key].append({'engagement all':'Anonymity'})
mainDct[key].append({'nps all': 'Anonymity'})
mainDct[key].append({'participation all': 'Anonymity'})
else:
mainDct[key].append({'engagement all': 'N/A'})
mainDct[key].append({'nps all':'N/A'})
mainDct[key].append({'participation all': 'N/A'})
if switch == False:
mainDct[key].append({'aSegID' : 'None'})
mainDct[key].append({'engagement all' :'N/A'})
mainDct[key].append({'nps all': 'N/A'})
mainDct[key].append({'participation all: N/A'})
for key in mainDct:
switch = False
for i in mainDct[key]:
if 'dSegID' in i:
switch = True
data = self.getPeakData("[REMOVED]" % (i['dSegID']))
if 'mean' in data['data']['attributes']['scores']:
score = data['data']['attributes']['scores']['mean']
participation = data['data']['attributes']['participation']['mean']
nps = data['data']['attributes']['scores']['nps']['score']
mainDct[key].append({'engagement direct': score})
mainDct[key].append({'nps direct': nps})
mainDct[key].append({'participation direct': participation})
elif 'anonymity' in data['data']['attributes']['scores']:
mainDct[key].append({'engagement direct': 'Anonymity'})
mainDct[key].append({'nps direct': 'Anonymity'})
mainDct[key].append({'participation direct': 'Anonymity'})
else:
mainDct[key].append({'engagement direct': 'N/A'})
mainDct[key].append({'nps direct': 'N/A'})
mainDct[key].append({'participation direct': 'N/A'})
if switch == False:
mainDct[key].append({'dSegID' : 'None'})
mainDct[key].append({'engagement direct': 'N/A'})
mainDct[key].append({'nps direct': 'N/A'})
mainDct[key].append({'participation direct': 'N/A'})
with open('Output.csv', 'w') as csv_file:
csvwriter = csv.writer(csv_file, delimiter=',')
csvwriter.writerow(['Manager ID', 'Manager Name','Direct Engagement', 'All Engagement', 'Direct NPS', 'All NPS', 'Direct Participation', 'All Participation'])
for key in mainDct:
for i in mainDct[key]:
if 'name' in i:
name = i['name']
if 'EID' in i:
EID = i['EID']
if 'nps direct' in i:
ND = i['nps direct']
if 'engagement direct' in i:
ED = i['engagement direct']
if 'participation direct' in i:
PD = i['participation direct']
if 'engagement all' in i:
EA = i['engagement all']
if 'nps all' in i:
NA = i['nps all']
if 'participation all' in i:
PA = i['participation all']
csvwriter.writerow([EID,name,ED,EA,ND,NA,PD,PA])
def main():
thisSync = mgrData()
thisSync.PeakExport()
main()
Some errors I receive:
Please wait, data generating...
Traceback (most recent call last):
File "/Volumes/GoogleDrive/My Drive/BI Scripts/Random.py", line 209, in <module>
main()
File "/Volumes/GoogleDrive/My Drive/BI Scripts/Random.py", line 204, in main
thisSync.PeakExport()
File "/Volumes/GoogleDrive/My Drive/BI Scripts/Random.py", line 72, in PeakExport
hrisIDget = self.getPeakData("/scim/v2/users/employee_"+str(pkEmpID))
File "/Volumes/GoogleDrive/My Drive/BI Scripts/Random.py", line 31, in getPeakData
res = conn.getresponse()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1332, in getresponse
response.begin()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 303, in begin
version, status, reason = self._read_status()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 264, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socket.py", line 669, in readinto
return self._sock.recv_into(b)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 1241, in recv_into
return self.read(nbytes, buffer)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 1099, in read
return self._sslobj.read(len, buffer)
ConnectionResetError: [Errno 54] Connection reset by peer
[Finished in 1263.8s with exit code 1]
[cmd: ['/Library/Frameworks/Python.framework/Versions/3.8/bin/python3', '-u', '/Volumes/GoogleDrive/My Drive/BI Scripts/Random.py']]
[dir: /Volumes/GoogleDrive/My Drive/BI Scripts]
[path: /Library/Frameworks/Python.framework/Versions/3.8/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin]
Please wait, data generating...
Traceback (most recent call last):
File "/Volumes/GoogleDrive/My Drive/BI Scripts/Random.py", line 209, in <module>
main()
File "/Volumes/GoogleDrive/My Drive/BI Scripts/Random.py", line 204, in main
thisSync.PeakExport()
File "/Volumes/GoogleDrive/My Drive/BI Scripts/Random.py", line 72, in PeakExport
hrisIDget = self.getPeakData("/scim/v2/users/employee_"+str(pkEmpID))
File "/Volumes/GoogleDrive/My Drive/BI Scripts/Random.py", line 31, in getPeakData
res = conn.getresponse()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1332, in getresponse
response.begin()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 303, in begin
version, status, reason = self._read_status()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 264, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socket.py", line 669, in readinto
return self._sock.recv_into(b)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 1241, in recv_into
return self.read(nbytes, buffer)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 1099, in read
return self._sslobj.read(len, buffer)
TimeoutError: [Errno 60] Operation timed out
Related
My application was running well but as I upgraded it to python version 3.4 . I am getting the error shown bellow. Looking at the error message I am not exactly able to debug the problem
Error:
--- Logging error ---
Traceback (most recent call last):
File "/usr/lib64/python3.4/logging/__init__.py", line 978, in emit
msg = self.format(record)
File "/usr/lib64/python3.4/logging/__init__.py", line 828, in format
return fmt.format(record)
File "/usr/lib64/python3.4/logging/__init__.py", line 573, in format
record.exc_text = self.formatException(record.exc_info)
File "/usr/lib64/python3.4/logging/__init__.py", line 523, in formatException
traceback.print_exception(ei[0], ei[1], tb, None, sio)
File "/usr/lib64/python3.4/traceback.py", line 169, in print_exception
for line in _format_exception_iter(etype, value, tb, limit, chain):
File "/usr/lib64/python3.4/traceback.py", line 146, in _format_exception_iter
for value, tb in values:
File "/usr/lib64/python3.4/traceback.py", line 125, in _iter_chain
context = exc.__context__
AttributeError: 'NoneType' object has no attribute '__context__'
Call stack:
File "main.py", line 253, in <module>
main()
File "main.py", line 144, in main
plugininfo.plugin_object.run(cons.MAIN_CONFIG_PATH, outputpath, finallogs, plugininfo.name, args.inventory)
File "/home/sjoshi/python3.4/check-acess/plugins/plugin_etc_passwd/plugin_etc_passwd.py", line 107, in run
self.result = phelper.executeCommand(runcommand)
File "/home/sjoshi/python3.4/check-acess/lib/sshplugin/sshplugin.py", line 86, in executeCommand
logging.exception("ErrorCode:%d %s", cmdout.returncode, output)
Message: 'ErrorCode:%d %s'
Arguments: (255, b'')
No Result found or all users filtered sbx32 /etc/passwd
Permission denied (publickey).
--- Logging error ---
Traceback (most recent call last):
File "/usr/lib64/python3.4/logging/__init__.py", line 978, in emit
msg = self.format(record)
File "/usr/lib64/python3.4/logging/__init__.py", line 828, in format
return fmt.format(record)
File "/usr/lib64/python3.4/logging/__init__.py", line 573, in format
record.exc_text = self.formatException(record.exc_info)
File "/usr/lib64/python3.4/logging/__init__.py", line 523, in formatException
traceback.print_exception(ei[0], ei[1], tb, None, sio)
File "/usr/lib64/python3.4/traceback.py", line 169, in print_exception
for line in _format_exception_iter(etype, value, tb, limit, chain):
File "/usr/lib64/python3.4/traceback.py", line 146, in _format_exception_iter
for value, tb in values:
File "/usr/lib64/python3.4/traceback.py", line 125, in _iter_chain
context = exc.__context__
AttributeError: 'NoneType' object has no attribute '__context__'
Call stack:
File "main.py", line 253, in <module>
main()
File "main.py", line 144, in main
plugininfo.plugin_object.run(cons.MAIN_CONFIG_PATH, outputpath, finallogs, plugininfo.name, args.inventory)
File "/home/sjoshi/python3.4/check-acess/plugins/plugin_etc_passwd/plugin_etc_passwd.py", line 107, in run
self.result = phelper.executeCommand(runcommand)
File "/home/sjoshi/python3.4/check-acess/lib/sshplugin/sshplugin.py", line 86, in executeCommand
logging.exception("ErrorCode:%d %s", cmdout.returncode, output)
Message: 'ErrorCode:%d %s'
Arguments: (255, b'')
sshplugin.py
import os
import subprocess as subp
import logging
import lib.exceptions.errors as error
import lib.inventory.bashhostlist as hostname
class SSHPlugin:
"""
Helper class for the SSH based plugins
"""
def makeCommand(self, user, host, filepath, sudo, timeout, attempt, changetols=None):
"""
-Two types of commands with or without ssh depends on hostname
-Other depends on parameter set or unset
-ConnectionAttempts and ConnectTimeout to occur must be SSH
-last arg(changetols): This is to checkif the plugin type is authorized_keys. Because in that case we need ls not cat
"""
command=""
if changetols is None:
command = ["cat", filepath]
else:
command=["ls", filepath]
if host.lower() == 'localhost':
if sudo.lower() == 'yes':
command.insert(0, 'sudo')
else:
command.insert(0, "ssh")
if timeout and not attempt:
command.insert(1, '-o')
command.insert(
2, "ConnectTimeout={timeout}".format(timeout=timeout))
command.insert(3, user+"#"+host)
if sudo.lower() == 'yes':
command.insert(4, 'sudo')
elif timeout and attempt:
command.insert(1, '-o')
command.insert(
2, "ConnectTimeout={timeout}".format(timeout=timeout))
command.insert(3, '-o')
command.insert(
4, "ConnectionAttempts={att}".format(att=attempt))
command.insert(5, user+"#"+host)
if sudo.lower() == 'yes':
command.insert(6, 'sudo')
elif attempt and not timeout:
command.insert(1, '-o')
command.insert(
2, "ConnectionAttempts={att}".format(att=attempt))
command.insert(3, user+"#"+host)
if sudo.lower() == 'yes':
command.insert(4, 'sudo')
else:
command.insert(1, user+"#"+host)
if sudo.lower() == 'yes':
command.insert(2, 'sudo')
return command
def executeCommand(self, command, filtercommand=None):
"""
Multuple Popen used to execute the process,Command followed by filter if any
if no filter than just a single commad run else
The output of first will act as the input to the filtering command
"""
if filtercommand is None:
cmdout = subp.Popen(command, stdout=subp.PIPE)
output, err = cmdout.communicate()
if cmdout.returncode is 0:
logging.info("Result success,status code %d", cmdout.returncode)
return output
else:
logging.exception("ErrorCode:%d %s %s", cmdout.returncode, output,err)
return False
else:
cmdout = subp.Popen(command, stdout=subp.PIPE)
filtered = subp.Popen(filtercommand, stdin=cmdout.stdout, stdout=subp.PIPE)
output, err = filtered.communicate()
if filtered.returncode is 0:
logging.info("Result success,status code %d", filtered.returncode)
return output
else:
logging.exception("ErrorCode:%d %s", filtered.returncode, output)
return False
why is the logging.exception throwing errors. Is it the problem with the logging module. I tried to replace the logging.exception with logging.info instead but that didn't work.
Here is my code
import os
import glob
import time
import sys
import datetime
import urllib2
baseURL = "https://api.thingspeak.com/update?api_key=DCL2IZ1REQT5GUSM"
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = 'sys/bus/w1/devices/'
#Temp1 temperature device location
temp1_file = '/sys/bus/w1/devices/28-0000087787c4/w1_slave'
#Temp2 temperature Device Location
temp2_file = '/sys/bus/w1/devices/28-000008762fa3/w1_slave'
#Determine Temp1 Temperature
def read_rawtemp_temp1():
y = open(temp1_file,'r')
lines = y.readlines()
y.close
return lines
def read_temp_temp1():
lines = read_rawtemp_temp1()
while lines[0].strip()[-3:] !='YES':
time.sleep(0.2)
lines = read_rawtemp_temp1()
equals_pos = lines[1].find('t=')
if equals_pos !=-1:
temp_string = lines[1][equals_pos+2:]
temp_temp1 = ((float(temp_string)/1000.0)*1.8)-32
return temp_temp1
#Determine Temp2 Temperature
def read_rawtemp_temp2():
x = open(temp2_file,'r')
lines = x.readlines()
x.close
return lines
def read_temp_temp2():
lines = read_rawtemp_temp2()
while lines[0].strip()[-3:] !='YES':
time.sleep(0.2)
lines = read_rawtemp_temp1()
equals_pos = lines[1].find('t=')
if equals_pos !=-1:
temp_string = lines[1][equals_pos+2:]
temp_temp2 = float(temp_string)/1000.0
return temp_temp2
while True: #Loop
#Send Temp1 Temperature to Thingspeak
temp1_tempin = read_temp_temp1()
#Send temp2 temperature to Thingspeak
temp2_tempin = read_temp_temp2()
#Pull results together
values = [datetime.datetime.now(), temp1_tempin, temp2_tempin]
#Open Thingspeak channel and assign fields to temperatures
j = urllib2.urlopen(baseURL + "&field1=%s" % (temp1_tempin) + "&field2=%s" % (temp2_tempin))
try:
search_response = urllib2.urlopen(search_request)
except urllib2.HTTPError:
pass
#Time to next loop
time.sleep(600)
Here is the error that will stop the script from running after a few hours.
Traceback (most recent call last):
File "tempdatalog.py", line 137, in <module>
j = urllib2.urlopen(baseURL + "&field1=%s" % (temp1_tempin) + "&field2=%s" % (temp2_tempin))
File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 437, in open
response = meth(req, response)
File "/usr/lib/python2.7/urllib2.py", line 550, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.7/urllib2.py", line 475, in error
return self._call_chain(*args)
File "/usr/lib/python2.7/urllib2.py", line 409, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 558, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 500: Internal Server Error
I added:
try:
search_response = urllib2.urlopen(search_request)
except urllib2.HTTPError:
pass
this to try to get it to pass over the error, but it didn't work. Any advice that would be given would be great, this is just the start to a monitor for a medical system in a mobile.
Looking at your stacktrace, it looks like you wrapped the wrong line in the try/except clause.
File "tempdatalog.py", line 137, in
j = urllib2.urlopen(baseURL + "&field1=%s" % (temp1_tempin) +
"&field2=%s" % (temp2_tempin))
This version at least catches the exception:
#Open Thingspeak channel and assign fields to temperatures
try:
j = urllib2.urlopen(baseURL + "&field1=%s" % (temp1_tempin) + "&field2=%s" % (temp2_tempin))
search_response = urllib2.urlopen(search_request)
except urllib2.HTTPError:
pass
That still does not explain why the error appears in the first place. My guess would be a form of rate limiting
I am running a python script with Phontomjs and Selenium. I am facing timeout issue. It is stopping after 20-50min. I need a solution so that I can run my script without this timeout issue. where is the problem please and how can I solve it?
The input file cannot be read or no in proper format.
Traceback (most recent call last):
File "links_crawler.py", line 147, in <module>
crawler.Run()
File "links_crawler.py", line 71, in Run
self.checkForNextPages()
File "links_crawler.py", line 104, in checkForNextPages
self.next.click()
File "/home/dev/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 75, in click
self._execute(Command.CLICK_ELEMENT)
File "/home/dev/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 454, in _execute
return self._parent.execute(command, params)
File "/home/dev/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 199, in execute
response = self.command_executor.execute(driver_command, params)
File "/home/dev/.local/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 395, in execute
return self._request(command_info[0], url, body=data)
File "/home/dev/.local/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 463, in _request
resp = opener.open(request, timeout=self._timeout)
File "/usr/lib/python2.7/urllib2.py", line 431, in open
response = self._open(req, data)
File "/usr/lib/python2.7/urllib2.py", line 449, in _open
'_open', req)
File "/usr/lib/python2.7/urllib2.py", line 409, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1227, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/usr/lib/python2.7/urllib2.py", line 1200, in do_open
r = h.getresponse(buffering=True)
File "/usr/lib/python2.7/httplib.py", line 1127, in getresponse
response.begin()
File "/usr/lib/python2.7/httplib.py", line 453, in begin
version, status, reason = self._read_status()
File "/usr/lib/python2.7/httplib.py", line 417, in _read_status
raise BadStatusLine(line)
httplib.BadStatusLine: ''
Code:
class Crawler():
def __init__(self,where_to_save, verbose = 0):
self.link_to_explore = ''
self.TAG_RE = re.compile(r'<[^>]+>')
self.TAG_SCRIPT = re.compile(r'<(script).*?</\1>(?s)')
if verbose == 1:
self.driver = webdriver.Firefox()
else:
self.driver = webdriver.PhantomJS()
self.links = []
self.next = True
self.where_to_save = where_to_save
self.logs = self.where_to_save + "/logs"
self.outputs = self.where_to_save + "/outputs"
self.logfile = ''
self.rnd = 0
try:
os.stat(self.logs)
except:
os.makedirs(self.logs)
try:
os.stat(self.outputs)
except:
os.makedirs(self.outputs)
try:
fin = open(file_to_read,"r")
FileContent = fin.read()
fin.close()
crawler =Crawler(where_to_save)
data = FileContent.split("\n")
for info in data:
if info!="":
to_process = info.split("|")
link = to_process[0].strip()
category = to_process[1].strip().replace(' ','_')
print "Processing the link: " + link : " + info
crawler.Init(link,category)
crawler.Run()
crawler.End()
crawler.closeSpider()
except:
print "The input file cannot be read or no in proper format."
raise
If you don't want Timeout to stop your script you can catch the exception
selenium.common.exceptions.TimeoutException and pass it.
You can set the default page load timeout using the set_page_load_timeout() method of webdriver.
Like this
driver.set_page_load_timeout(10)
This will throw a TimeoutException if your page didn't load in 10 seconds.
EDIT:
Forgot to mention that you will have to put your code in a loop.
Add import
from selenium.common.exceptions import TimeoutException
while True:
try:
# Your code here
break # Loop will exit
except TimeoutException:
pass
Hello I am trying to use the google places api to get a list of all the McDonalds in Germany (weird exercise I know). This is my code (the box is smaller than the actual box for Germany so it does not take a long time to iterate):
import urllib2, simplejson
GOOGLE_API_KEY = 'my google api'
lat_NW = 53.4017872352
lng_NW = 5.954233125
lat_SE = 51.9766032969
lng_SE = 10.032130575
lat_curr = lat_NW
lng_curr = lng_NW
total_calls = 0
lat_incr = -.29
lng_incr = .29
while lng_curr<lng_SE:
lng_curr = lng_NW
while lat_curr > lng_SE:
curr_location = str(lat_curr) + "," + str(lng_curr)
url='https://maps.googleapis.com/maps/api/place/search/json?location=' + curr_location + '&sensor=false&key=' + GOOGLE_API_KEY + '&radius=20000&types=restaurant&name=mcdonalds'
response = urllib2.urlopen(url)
result = response.read()
d = simplejson.loads(result)
lng_curr += lng_incr
lat_curr += lat_incr
print (d)
And this is the output I got:
Traceback (most recent call last):
File "<stdin>", line 7, in <module>
File "F:\WinPython-64bit-2.7.6.3\python-2.7.6.amd64\lib\urllib2.py", line 127, in urlopen
return _opener.open(url, data, timeout)
File "F:\WinPython-64bit-2.7.6.3\python-2.7.6.amd64\lib\urllib2.py", line 404, in open
response = self._open(req, data)
File "F:\WinPython-64bit-2.7.6.3\python-2.7.6.amd64\lib\urllib2.py", line 422, in _open
'_open', req)
File "F:\WinPython-64bit-2.7.6.3\python-2.7.6.amd64\lib\urllib2.py", line 382, in _call_chain
result = func(*args)
File "F:\WinPython-64bit-2.7.6.3\python-2.7.6.amd64\lib\urllib2.py", line 1222, in https_open
return self.do_open(httplib.HTTPSConnection, req)
File "F:\WinPython-64bit-2.7.6.3\python-2.7.6.amd64\lib\urllib2.py", line 1184, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [Errno 8] _ssl.c:507: EOF occurred in violation of protocol>
Any ideas on what am I doing wrong? Thanks!
My guess would be you've reached the usage limit.
If you try this, you'll see that your loop does never abort:
lat_NW = 53.4017872352
lng_NW = 5.954233125
lat_SE = 51.9766032969
lng_SE = 10.032130575
lat_curr = lat_NW
lng_curr = lng_NW
total_calls = 0
lat_incr = -.29
lng_incr = .29
while lng_curr<lng_SE:
lng_curr = lng_NW
while lat_curr > lng_SE:
curr_location = str(lat_curr) + "," + str(lng_curr)
print curr_location
lng_curr += lng_incr
lat_curr += lat_incr
What you probably want is something like this:
...
while lng_curr < lng_SE:
lat_curr = lat_NW
while lat_curr > lat_SE:
curr_location = str(lat_curr) + "," + str(lng_curr)
...
lat_curr += lat_incr
lng_curr += lng_incr
1 upload_odl function
import os
import urllib2_files
import urllib2
user = 'patrick'
password = 'mypass'
url = 'http://localhost:8000/api/odl/'
password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(
None, url, user, password
)
auth_handler = urllib2.HTTPBasicAuthHandler(password_manager)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
def upload_odl(filename, sky_code=46800):
f = open(filename)
params = {
'installer_ac': str(sky_code),
'pdf': {
'fd': f,
'filename': os.path.basename(filename),
},
}
try:
the_page = urllib2.urlopen(url, params)
#response = urllib2.urlopen(req)
#the_page = response.read()
except urllib2.HTTPError, error:
print error.read()
and this second snippet that search and uploads some documents
import os
import time
from rest import upload_odl
start_time = time.time()
ODL_DIRECTORY = '/Users/patrick/Documents/ODL'
UPLOAD_DIR = 'CARICA'
directories = os.listdir(ODL_DIRECTORY)
files = 0
try:
for dir in directories:
c = os.path.join(ODL_DIRECTORY, dir)
if os.path.isdir(c):
u = os.path.join(c, UPLOAD_DIR)
if not os.path.exists(u):
continue
for x in os.listdir(u):
upload_odl(os.path.join(u, x))
#time.sleep(1)
files += 1
print 'Uploaded %d files in ' % len(to_upload),
print time.time() - start_time, " seconds"
except IOError, e:
#edit
import sys, import traceback
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exception(exc_type, exc_value, exc_traceback)
print e, os.path.join(u, x)
when I run the code, I get this error, after 5 uploads:
patrick:odl_uploader patrick$ python __init__.py
[Errno 32] Broken pipe /Users/patrick/Documents/ODL/name.pdf
EDIT:
Traceback output:
File "__init__.py", line 25, in <module>
upload_odl(os.path.join(u, x))
File "/Users/patrick/Documents/django/installer/installer_crm/tools/odl_uploader/rest/__init__.py", line 32, in upload_odl
the_page = urllib2.urlopen(url, params)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 124, in urlopen
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 383, in open
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 401, in _open
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 361, in _call_chain
File "/Users/patrick/Documents/django/installer/installer_crm/tools/rest/urllib2_files.py", line 207, in http_open
File "/Users/patrick/Documents/django/installer/installer_crm/tools/rest/urllib2_files.py", line 281, in do_open
File "/Users/patrick/Documents/django/installer/installer_crm/tools/rest/urllib2_files.py", line 194, in send_data
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/httplib.py", line 719, in send
File "<string>", line 1, in sendall
[Errno 32] Brok
en pipe /Users/patrick/Documents/ODL/name.pdf