programing

Python이 woocommerce rest api와 연결할 수 없습니다.

showcode 2023. 3. 21. 22:43
반응형

Python이 woocommerce rest api와 연결할 수 없습니다.

python을 사용하여 mongodb에서 제품 데이터를 전달하는 woocommerce rest api 작업을 하고 있습니다.저도 같은 내용의 대본을 썼어요.처음에 스크립트가 정상적으로 실행되어 2개의 제품을 통과했는데, 더 많은 제품을 통과하려고 하면 실패합니다.어디서 잘못하고 있는지 모르겠어요.

다음 오류 표시:

요청한다.예외입니다.ReadTimeout: HTTPConnectionPool(host='localhost', 포트=80): 읽기 시간이 초과되었습니다.(읽기 시간 초과=5)

스크립트:

from woocommerce import API
import os, sys
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(parent_dir)


from portal_v2.settings import _MONGO_DB_URI
from pymongo import MongoClient
import os, csv, ast

wcapi = API(
    url="http://localhost/wordpress1",
    consumer_key="ck_******************************************",
    consumer_secret="cs_******************************************",
    wp_api=True,
    version="wc/v1"
)



class Products(object):
    def __init__(self, dbname):
        _client = MongoClient(_MONGO_DB_URI)
        self._database = _client[dbname]
        self.getCollections()

        self.dbname = dbname

    def getCollections(self):
        self.products = self._database['products']

    def getProducts(self):
        product_dict = []
        for each in self.products.find().limit(10):
            product_dict.append({"name": each['name'],"slug": each['name'].replace(' ','').replace('/','') + each['sku'].replace(' ','').replace('/',''),"type": "simple","status": "publish","featured": False,"catalog_visibility": "visible","description": each['description'],"sku": each['sku'],"regular_price": each['mrp'],"sale_price": each['cost_price'],"date_on_sale_from": "","date_on_sale_to": "","purchasable": True,"total_sales": 0,"virtual": False,"downloadable": False,"downloads": [],"download_limit": -1,"download_expiry": -1,"download_type": "standard","external_url": "","button_text": "","tax_status": "taxable","tax_class": "","manage_stock": False,"stock_quantity": None,"in_stock": True,"backorders": "no","backorders_allowed": False,"backordered": False, "sold_individually": False, "weight": each['weight_gms'],"dimensions": {"length": each['length_mm'],"width": each['width_mm'],"height": each['height_mm']},"shipping_required": True,"shipping_taxable": True,"shipping_class": "", "shipping_class_id": 0,"reviews_allowed": True,"average_rating": "0.00","rating_count": 0,"related_ids": [],"upsell_ids": [],"cross_sell_ids": [],"parent_id": 0,"purchase_note": "","categories": [{"id": 9,},{"id": 14,}],"tags": [],"images": [{"src": each['image_url'].replace('dl=0','raw=1'),"name": each['name'],"position": 0}],"attributes": [],"default_attributes": [],"variations": [],"menu_order": 0})

        data = {'create':product_dict}


        print data
        return data

    def updateProducts(self, data):
        wcapi.post("products/batch", data)
        # print wcapi.get("products/?per_page=45").json()


data = Products('xyz').getProducts()
Products('xyz').updateProducts(data)

여기서 'xyz'는 데이터베이스 이름입니다.

타임아웃 옵션을 늘리기만 하면 됩니다.예를 들어 다음과 같습니다.

wcapi = API(
url="http://localhost/wordpress1",
consumer_key="ck_******************************************",
consumer_secret="cs_******************************************",
wp_api=True,
version="wc/v1",
timeout=10 # the default is 5, increase to whatever works for you.

)

언급URL : https://stackoverflow.com/questions/39545439/python-unable-to-make-connection-with-woocommerce-rest-api

반응형