How to debug socket error - python

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

Related

Selenium ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

I have a python script, which is used to scrape images from google. To run the script, you need to create a file named imgsearch_list.txt where you have to pass a list into that like cat, dog so as to search that list on google.
Where i run the script it throws me an error.
You can see the code:
import re, os, sys, datetime, time
import pandas
from selenium import webdriver
from contextlib import closing
from selenium.webdriver import Chrome
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from pattern.web import URL, extension, cache, plaintext, Newsfeed, DOM
class GoogleImageExtractor(object):
def __init__(self, search_key = '' ):
""" Google image search class
Args:
search_key to be entered.
"""
if type(search_key) == str:
## convert to list even for one search keyword to standalize the pulling.
self.g_search_key_list = [search_key]
elif type(search_key) == list:
self.g_search_key_list = search_key
else:
print ('google_search_keyword not of type str or list')
raise
self.g_search_key = ''
## user options
self.image_dl_per_search = 200
## url construct string text
self.prefix_of_search_url = "https://www.google.com.sg/search?q="
self.postfix_of_search_url = '&source=lnms&tbm=isch&sa=X&ei=0eZEVbj3IJG5uATalICQAQ&ved=0CAcQ_AUoAQ&biw=939&bih=591'# non changable text
self.target_url_str = ''
## storage
self.pic_url_list = []
self.pic_info_list = []
## file and folder path
self.folder_main_dir_prefix = r'C:\Users\intel\Desktop\Scrappr'
def reformat_search_for_spaces(self):
"""
Method call immediately at the initialization stages
get rid of the spaces and replace by the "+"
Use in search term. Eg: "Cookie fast" to "Cookie+fast"
steps:
strip any lagging spaces if present
replace the self.g_search_key
"""
self.g_search_key = self.g_search_key.rstrip().replace(' ', '+')
def set_num_image_to_dl(self, num_image):
""" Set the number of image to download. Set to self.image_dl_per_search.
Args:
num_image (int): num of image to download.
"""
self.image_dl_per_search = num_image
def get_searchlist_fr_file(self, filename):
"""Get search list from filename. Ability to add in a lot of phrases.
Will replace the self.g_search_key_list
Args:
filename (str): full file path
"""
with open(filename,'r') as f:
self.g_search_key_list = f.readlines()
def formed_search_url(self):
''' Form the url either one selected key phrases or multiple search items.
Get the url from the self.g_search_key_list
Set to self.sp_search_url_list
'''
self.reformat_search_for_spaces()
self.target_url_str = self.prefix_of_search_url + self.g_search_key +\
self.postfix_of_search_url
def retrieve_source_fr_html(self):
""" Make use of selenium. Retrieve from html table using pandas table.
"""
driver = webdriver.Chrome(executable_path=r"C:\Users\intel\Downloads\setups\chromedriver.exe")
driver.get(self.target_url_str)
## wait for log in then get the page source.
try:
driver.execute_script("window.scrollTo(0, 30000)")
time.sleep(2)
self.temp_page_source = driver.page_source
#driver.find_element_by_css_selector('ksb _kvc').click()#cant find the class
driver.find_element_by_id('smb').click() #ok
time.sleep(2)
driver.execute_script("window.scrollTo(0, 60000)")
time.sleep(2)
driver.execute_script("window.scrollTo(0, 60000)")
except:
print ('not able to find')
driver.quit()
self.page_source = driver.page_source
driver.close()
def extract_pic_url(self):
""" extract all the raw pic url in list
"""
dom = DOM(self.page_source)
tag_list = dom('a.rg_l')
for tag in tag_list[:self.image_dl_per_search]:
tar_str = re.search('imgurl=(.*)&imgrefurl', tag.attributes['href'])
try:
self.pic_url_list.append(tar_str.group(1))
except:
print ('error parsing', tag)
def multi_search_download(self):
""" Mutli search download"""
for indiv_search in self.g_search_key_list:
self.pic_url_list = []
self.pic_info_list = []
self.g_search_key = indiv_search
self.formed_search_url()
self.retrieve_source_fr_html()
self.extract_pic_url()
self.downloading_all_photos() #some download might not be jpg?? use selnium to download??
self.save_infolist_to_file()
def downloading_all_photos(self):
""" download all photos to particular folder
"""
self.create_folder()
pic_counter = 1
for url_link in self.pic_url_list:
print (pic_counter)
pic_prefix_str = self.g_search_key + str(pic_counter)
self.download_single_image(url_link.encode(), pic_prefix_str)
pic_counter = pic_counter +1
def download_single_image(self, url_link, pic_prefix_str):
""" Download data according to the url link given.
Args:
url_link (str): url str.
pic_prefix_str (str): pic_prefix_str for unique label the pic
"""
self.download_fault = 0
file_ext = os.path.splitext(url_link)[1] #use for checking valid pic ext
temp_filename = pic_prefix_str + file_ext
temp_filename_full_path = os.path.join(self.gs_raw_dirpath, temp_filename )
valid_image_ext_list = ['.png','.jpg','.jpeg', '.gif', '.bmp', '.tiff'] #not comprehensive
url = URL(url_link)
if url.redirect:
return # if there is re-direct, return
if file_ext not in valid_image_ext_list:
return #return if not valid image extension
f = open(temp_filename_full_path, 'wb') # save as test.gif
print (url_link)
self.pic_info_list.append(pic_prefix_str + ': ' + url_link )
try:
f.write(url.download())#if have problem skip
except:
#if self.__print_download_fault:
print ('Problem with processing this data: ', url_link)
self.download_fault =1
f.close()
def create_folder(self):
"""
Create a folder to put the log data segregate by date
"""
self.gs_raw_dirpath = os.path.join(self.folder_main_dir_prefix, time.strftime("_%d_%b%y", time.localtime()))
if not os.path.exists(self.gs_raw_dirpath):
os.makedirs(self.gs_raw_dirpath)
def save_infolist_to_file(self):
""" Save the info list to file.
"""
temp_filename_full_path = os.path.join(self.gs_raw_dirpath, self.g_search_key + '_info.txt' )
with open(temp_filename_full_path, 'w') as f:
for n in self.pic_info_list:
f.write(n)
f.write('\n')
if __name__ == '__main__':
choice =4
if choice ==4:
"""test the downloading of files"""
w = GoogleImageExtractor('')#leave blanks if get the search list from file
searchlist_filename = r'C:\Users\intel\Desktop\Scrappr\imgsearch_list.txt'
w.set_num_image_to_dl(200)
w.get_searchlist_fr_file(searchlist_filename)#replace the searclist
w.multi_search_download()
Here's the error:
not able to find
Traceback (most recent call last):
File "C:\Users\intel\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 156, in _new_conn
conn = connection.create_connection(
File "C:\Users\intel\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\connection.py", line 84, in create_connection
raise err
File "C:\Users\intel\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\connection.py", line 74, in create_connection
sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\intel\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 665, in urlopen
httplib_response = self._make_request(
File "C:\Users\intel\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 387, in _make_request
conn.request(method, url, **httplib_request_kw)
File "C:\Users\intel\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 1230, in request
self._send_request(method, url, body, headers, encode_chunked)
File "C:\Users\intel\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 1276, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "C:\Users\intel\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 1225, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "C:\Users\intel\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 1004, in _send_output
self.send(msg)
File "C:\Users\intel\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 944, in send
self.connect()
File "C:\Users\intel\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 184, in connect
conn = self._new_conn()
File "C:\Users\intel\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 168, in _new_conn
raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x0000000007017520>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:/Users/intel/Desktop/go.py", line 211, in <module>
w.multi_search_download()
File "c:/Users/intel/Desktop/go.py", line 133, in multi_search_download
self.retrieve_source_fr_html()
File "c:/Users/intel/Desktop/go.py", line 106, in retrieve_source_fr_html
self.page_source = driver.page_source
File "C:\Users\intel\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 679, in page_source
return self.execute(Command.GET_PAGE_SOURCE)['value']
File "C:\Users\intel\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 319, in execute
response = self.command_executor.execute(driver_command, params)
File "C:\Users\intel\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 374, in execute
return self._request(command_info[0], url, body=data)
File "C:\Users\intel\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 397, in _request
resp = self._conn.request(method, url, body=body, headers=headers)
File "C:\Users\intel\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\request.py", line 75, in request
return self.request_encode_url(
File "C:\Users\intel\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\request.py", line 97, in request_encode_url
return self.urlopen(method, url, **extra_kw)
File "C:\Users\intel\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\poolmanager.py", line 330, in urlopen
response = conn.urlopen(method, u.request_uri, **kw)
File "C:\Users\intel\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 747, in urlopen
return self.urlopen(
File "C:\Users\intel\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 747, in urlopen
return self.urlopen(
File "C:\Users\intel\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 747, in urlopen
return self.urlopen(
File "C:\Users\intel\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 719, in urlopen
retries = retries.increment(
File "C:\Users\intel\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\retry.py", line 436, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=50181): Max retries exceeded with url: /session/a473cdecf0cbd7a585ac13d08f156b4a/source (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000000007017520>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))
Any help would be appreciated...

Timeout issue while running python script Phantomjs and Selenium

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

UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)

I have a Python script which uses tinypng api to convert images recursively and for some reason it does not work and I get:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal
not in range(128)
What am I doing wrong?
import os
import base64
from os.path import dirname
from urllib2 import Request, urlopen
from base64 import b64encode
compress_png = True
compress_jpg = True
import_dir = '666\product'
output_dir = '666\product'
tiny_png_key = 'xxxxxx'
tiny_png_url = 'https://api.tinypng.com/shrink'
img_count = 0
file_count = 0
compress_count = 0
existing_count = 0
def compressImage(filepath, filedest, overwrite = True):
global compress_count
global existing_count
if not os.path.isfile(filedest) or overwrite:
status = ''
request = Request(tiny_png_url, open(filepath, "rb").read())
auth = b64encode(bytes("api:" + tiny_png_key)).decode("ascii")
request.add_header("Authorization", "Basic %s" % auth)
response = urlopen(request)
if response.getcode() == 201:
status = "success";
headers = response.info()
result = urlopen(headers["Location"]).read()
if not os.path.exists(os.path.dirname(filedest)):
os.makedirs(os.path.dirname(filedest))
open(filedest, "wb").write(result)
compress_count += 1
else:
status = "failed"
print 'Compressing: %s\nFile: %s\nStatus: %s\n'%(filepath, img_count, status)
else:
existing_count += 1
# loop througs files in import_dir recursively
for subdir, dirs, files in os.walk(import_dir):
for file in files:
filepath = os.path.join(subdir, file)
fileName, fileExtension = os.path.splitext(file)
file_count += 1
if(fileExtension == '.png' and compress_png) or (fileExtension == '.jpg' and compress_jpg):
img_count += 1
filedest = filepath.replace(import_dir, output_dir)
compressImage(filepath, filedest)
print '================'
print 'Total Files: %s'%(file_count)
print 'Total Images: %s'%(img_count)
print 'Images Compressed: %s'%(compress_count)
print 'Images Previously Compressed (Exist in output directory): %s'%(existing_count)
Full error:
Traceback (most recent call last):
File "C:\Users\Vygantas\Desktop\test.py", line 55, in <module> compressImage(filepath, filedest)
File "C:\Users\Vygantas\Desktop\test.py", line 28, in compressImage response = urlopen(request)
File "C:\Python27\lib\urllib2.py", line 126, in urlopen return _opener.open(url, data, timeout)
File "C:\Python27\lib\urllib2.py", line 391, in open response = self._open(req, data)
File "C:\Python27\lib\urllib2.py", line 409, in _open '_open', req)
File "C:\Python27\lib\urllib2.py", line 369, in _call_chain result = func(*args)
File "C:\Python27\lib\urllib2.py", line 1181, in https_open return self.do_open(httplib.HTTPSConnection, req)
File "C:\Python27\lib\urllib2.py", line 1142, in do_open h.request(req.get_method(), req.get_selector(), req.data, headers)
File "C:\Python27\lib\httplib.py", line 946, in request self._send_request(method, url, body, headers)
File "C:\Python27\lib\httplib.py", line 987, in _send_request self.endheaders(body)
File "C:\Python27\lib\httplib.py", line 940, in endheaders self._send_output(message_body)
File "C:\Python27\lib\httplib.py", line 801, in _send_output msg += message_body UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)
Using the example on the website and adapting it to Python 2 the same way you did seems to work for me:
from os.path import dirname
from urllib2 import Request, urlopen
from base64 import b64encode
key = "xxxx_xxxx"
input = "NLMK.png"
output = "tiny-output.png"
request = Request("https://api.tinypng.com/shrink", open(input, "rb").read())
auth = b64encode("api:" + key).decode("ascii")
request.add_header("Authorization", "Basic %s" % auth)
response = urlopen(request)
if response.getcode() == 201:
# Compression was successful, retrieve output from Location header.
headers = response.info()
result = urlopen(headers["Location"]).read()
open(output, "wb").write(result)
else:
# Something went wrong! You can parse the JSON body for details.
print("Compression failed")
You have to encode the data but not the user name
I would try something like:
def compressImage(filepath, filedest, overwrite = True):
global compress_count
global existing_count
if not os.path.isfile(filedest) or overwrite:
status = ''
data = open(filepath, "rb").read()
data = base64.b64encode(data)
request = Request(tiny_png_url, data)
request.add_header("Content-type", "application/x-www-form-urlencoded; charset=UTF-8")
auth = "api:" + tiny_png_key
request.add_header("Authorization", "Basic %s" % auth)
response = urlopen(request)

404 being returned by urllib2.urlopen(req) even though HTTP 200 being sent

I have the below python script that is making a successful rest web service request but then throwing a 404 after the server sends a HTTP 200 back. I can see the rest web service web app log returning the below JSON response successfully. Any ideas on how to further troubleshoot this or what could be causing the below error?
Last Python line executed:
result = urllib2.urlopen(req)
JSON response from Rest API Endpoint:
response{"status":"SUCCESS","reason":null,"location":null,"details":null,"errorDetails":{}}
Python Script:
#!/home/user/activepython-2.7.2.5_x86_64/bin/python
import sys
import os
import logging
import subprocess
import tempfile
import json
import urllib2
# define SVN
svn_repo = sys.argv[1]
svn_txn = sys.argv[2]
svn_opt = '-t'
# handle temp file
tmp_file = tempfile.NamedTemporaryFile(prefix="app_",
suffix=".tmp", dir="/tmp", delete=False)
delete_file = True
rest_url = 'https://host/rest/api/endpoint'
# setup logging
log_level = logging.DEBUG
logger = logging.getLogger("myapp")
logger.setLevel(log_level)
handler = logging.StreamHandler(sys.stderr)
handler.setLevel(log_level)
logger.addHandler(handler)
def get_svn_changes():
cmd = "/home/user/bin/svnlook changed --copy-info %s %s %s" % (svn_opt, svn_txn, svn_repo)
output, return_code = command_output(cmd)
return output
def get_author():
cmd = "/home/csvn/bin/svnlook author %s %s %s" % (svn_opt, svn_txn, svn_repo)
author, return_code = command_output(cmd)
return author.strip()
def call_webservice():
req = urllib2.Request(rest_url)
req.add_header('Accept', 'arpplication/json')
req.add_header('Content-Type', 'application/json')
logger.debug("file=" + tmp_file.name)
data = json.dumps({"name" : "file", "value" : tmp_file.name})
logger.debug("data=" + data)
req.add_data(data)
logger.debug("request")
result = urllib2.urlopen(req)
logger.debug("result")
json_result = json.load(result)
logger.debug("json_result")
result_data = json.loads(json_result['response'])
logger.debug("result_data")
return result_data
if __name__ == "__main__":
exit_code = 0;
out_message = ''
author = get_author()
try:
tmp_file.write("author=%s\n" % author)
output = get_svn_changes()
tmp_file.write(output)
tmp_file.close()
output = call_webservice()
if (output['status'] == 'ERROR'):
out_message = output['reason']
exit_code = 1
except Exception, ex:
out_message = str(ex)
exit_code = 1
finally:
if (exit_code == 1):
sys.stderr.write("Error: %s" % out_message)
if delete_file:
os.remove(tmp_file.name)
sys.exit(exit_code)
Traceback Exception:
//startlogger output
file=/tmp/app_rrOgN0.tmp
data={"name": "file", "value": "/tmp/app_rrOgN0.tmp"}
request
//stop logger output
Traceback (most recent call last):
File "/home/csvn/data/repositories/repo/hooks/pre-commit", line 85, in <module>
output = call_webservice()
File "/home/csvn/data/repositories/repo/hooks/pre-commit", line 59, in call_webservice
result = urllib2.urlopen(req)
File "/home/activepython-2.7.2.5_x86_64/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/home/activepython-2.7.2.5_x86_64/lib/python2.7/urllib2.py", line 400, in open
response = meth(req, response)
File "/home/activepython-2.7.2.5_x86_64/lib/python2.7/urllib2.py", line 513, in http_response
'http', request, response, code, msg, hdrs)
File "/home/activepython-2.7.2.5_x86_64/lib/python2.7/urllib2.py", line 438, in error
return self._call_chain(*args)
File "/home/activepython-2.7.2.5_x86_64/lib/python2.7/urllib2.py", line 372, in _call_chain
result = func(*args)
File "/home/activepython-2.7.2.5_x86_64/lib/python2.7/urllib2.py", line 521, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 404: Not Found
Error: HTTP Error 404: Not Found
You misspelled the Accept header:
req.add_header('Accept', 'arpplication/json')
Correct the spelling of application:
req.add_header('Accept', 'application/json')

Python channeladvisor OrderService, suds.client.Client(url), urllib2.URLError: <urlopen error [Errno 54] Connection reset by peer>

At work I have to access/work with the Channel Advisor API
Source:
I'm attempting to perform a simple OrderService
from suds.client import Client
wsdl_url = 'https://api.channeladvisor.com/ChannelAdvisorAPI/v6/OrderService.asmx?WSDL'
service_url = 'https://api.channeladvisor.com/ChannelAdvisorAPI/v6/OrderService.asmx'
headers = {'Content-Type': 'text/xml; charset=utf-8'}
client = Client(wsdl_url, location = service_url, headers=headers)
Here is error:
Traceback (most recent call last):
File "test_suds.py", line 9, in <module>
client = Client(wsdl_url, location = service_url, headers=headers)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/suds-0.4.1-py2.7.egg/suds/client.py", line 112, in __init__
self.wsdl = reader.open(url)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/suds-0.4.1-py2.7.egg/suds/reader.py", line 152, in open
d = self.fn(url, self.options)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/suds-0.4.1-py2.7.egg/suds/wsdl.py", line 136, in __init__
d = reader.open(url)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/suds-0.4.1-py2.7.egg/suds/reader.py", line 79, in open
d = self.download(url)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/suds-0.4.1-py2.7.egg/suds/reader.py", line 95, in download
fp = self.options.transport.open(Request(url))
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/suds-0.4.1-py2.7.egg/suds/transport/https.py", line 60, in open
return HttpTransport.open(self, request)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/suds-0.4.1-py2.7.egg/suds/transport/http.py", line 62, in open
return self.u2open(u2request)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/suds-0.4.1-py2.7.egg/suds/transport/http.py", line 118, in u2open
return url.open(u2request, timeout=tm)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 404, in open
response = self._open(req, data)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 422, in _open
'_open', req)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 382, in _call_chain
result = func(*args)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1222, in https_open
return self.do_open(httplib.HTTPSConnection, req)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1184, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [Errno 54] Connection reset by peer>

Categories

Resources