How to fix HTTP Error 503: Service Unavailable error - bottlenose

Executing the command:
amazon = bottlenose.Amazon(AMAZON_ACCESS_KEY,AMAZON_SECRET_KEY,AMAZON_ASSOC_TAG)
response = amazon.ItemLookup(ItemId='B007OZNUCE')
I got the error:
Traceback (most recent call last):
File "exazapi.py", line 37, in <module>
response = amazon.ItemLookup(ItemId='B007OZNUCE')
File "C:\Python\lib\site-packages\bottlenose\api.py", line 274, in __call__
{'api_url': api_url, 'cache_url': cache_url})
File "C:\Python\lib\site-packages\bottlenose\api.py", line 235, in _call_api
return urllib2.urlopen(api_request, timeout=self.Timeout)
File "C:\Python\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "C:\Python\lib\urllib\request.py", line 531, in open
response = meth(req, response)
File "C:\Python\lib\urllib\request.py", line 641, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python\lib\urllib\request.py", line 569, in error
return self._call_chain(*args)
File "C:\Python\lib\urllib\request.py", line 503, in _call_chain
result = func(*args)
File "C:\Python\lib\urllib\request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 503: Service Unavailable
#import all the required modules
import re
import csv
import sys
import warnings
from urllib.request import urlopen
from bs4 import BeautifulSoup
from requests_html import HTMLSession
import bottlenose
#from amazon.api import AmazonAPI
AMAZON_ACCESS_KEY='xxx'
AMAZON_SECRET_KEY='xxx'
AMAZON_ASSOC_TAG='xxx'
#ignore warnings
if not sys.warnoptions:
warnings.simplefilter("ignore")
amazon = bottlenose.Amazon(AMAZON_ACCESS_KEY,AMAZON_SECRET_KEY,AMAZON_ASSOC_TAG)
response = amazon.ItemLookup(ItemId='B007OZNUCE')
print (amazon)
print(response)

Region is missing, add something like "DE" or "ES" depending on the country you're using.
Default is 'US':
amazon = bottlenose.Amazon(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_ASSOCIATE_TAG, Region='FR')
from: https://github.com/lionheart/bottlenose
Additionally: have you checked in the 'amazon' customer portal, on the 'platform API' access for the countries you want to use, that the API is enabled?
France: https://partenaires.amazon.fr/assoc_credentials/home
UK: https://affiliate-program.amazon.co.uk/assoc_credentials/home
US: https://affiliate-program.amazon.com/assoc_credentials/home
For me, it shows inactive, hence, that's probably the cause for the request to be denied.

Thanks for the inputs!
The same code worked magically now. Seems the api keys weren't enabled immediately after creating them.

Related

web3py EthereumTesterProvider - Basic interactions with a smart contract fail

On the web3py EthereumTesterProvider blockchain, I tested the contract deployment example at https://web3py.readthedocs.io/en/v5/contracts.html.
But I came across 2 errors.
pip config (windows 10) :
web3py (5.31.3)
eth-tester (0.8.0b3)
Here is the code :
from web3 import Web3
from solcx import compile_source
from pprint import pprint
# Solidity source code
compiled_sol = compile_source(
'''
pragma solidity ^0.8.17;
contract Greeter {
string public greeting;
constructor() public {
greeting = 'Hello';
}
function setGreeting(string memory _greeting) public {
greeting = _greeting;
}
function greet() view public returns (string memory) {
return greeting;
}
}
''',
output_values=['abi', 'bin']
)
# retrieve the contract interface
contract_id, contract_interface = compiled_sol.popitem()
# get bytecode and abi
bytecode = contract_interface['bin']
abi = contract_interface['abi']
# web3.py instance
w3 = Web3(Web3.EthereumTesterProvider())
# set pre-funded account as sender
w3.eth.default_account = w3.eth.accounts[0]
greeter_bin = w3.eth.contract(abi=abi, bytecode=bytecode)
# Submit the transaction that deploys the contract
tx_hash = greeter_bin.constructor().transact() # <==== first error
# tx_hash = greeter_bin.constructor().transact({'gas': 123456})
# Wait for the transaction to be mined, and get the transaction receipt
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
pprint(dict(tx_receipt))
greeter_obj = w3.eth.contract(address=tx_receipt.contractAddress, abi=abi)
print(f"{greeter_obj.functions.greet().call() = }") # <===== Second error
tx_hash = greeter_obj.functions.setGreeting('Nihao').transact()
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
print(f"{greeter_obj.functions.greet().call() = }")
1) The first error takes place at the contract deployment:
"TypeError: MockBackend.estimate_gas() takes 2 positional arguments but 3 were given."
I fixed it by adding a dictionary with some gas but the exemple from Web3.py does not have this parameter. I'd like to know the reason. Did I miss something?
C:\Users\Gilles\AppData\Local\Programs\Python\Python310\lib\site-packages\eth_tester\backends\__init__.py:30: UserWarning: Ethereum Tester: No backend was explicitly set, and no *full* backends were available. Falling back to the `MockBackend` which does not support all EVM functionality. Please refer to the `eth-tester` documentation for information on what backends are available and how to set them. Your py-evm package may need to be updated.
warnings.warn(
Traceback (most recent call last):
File "D:\_P\dev\python\blockchain\web3\tester1.py", line 45, in <module>
tx_hash = greeter_bin.constructor().transact()
File "C:\Users\Gilles\AppData\Local\Programs\Python\Python310\lib\site-packages\eth_utils\decorators.py", line 18, in _wrapper
return self.method(obj, *args, **kwargs)
.............
File "C:\Users\Gilles\AppData\Local\Programs\Python\Python310\lib\site-packages\web3\providers\eth_tester\middleware.py", line 331, in middleware
return make_request(method, [filled_transaction] + list(params)[1:])
File "C:\Users\Gilles\AppData\Local\Programs\Python\Python310\lib\site-packages\web3\middleware\formatting.py", line 94, in middleware
response = make_request(method, params)
File "C:\Users\Gilles\AppData\Local\Programs\Python\Python310\lib\site-packages\web3\providers\eth_tester\main.py", line 103, in make_request
response = delegator(self.ethereum_tester, params)
File "cytoolz\functoolz.pyx", line 253, in cytoolz.functoolz.curry.__call__
File "cytoolz\functoolz.pyx", line 249, in cytoolz.functoolz.curry.__call__
File "C:\Users\Gilles\AppData\Local\Programs\Python\Python310\lib\site-packages\web3\providers\eth_tester\defaults.py", line 66, in call_eth_tester
return getattr(eth_tester, fn_name)(*fn_args, **fn_kwargs)
File "C:\Users\Gilles\AppData\Local\Programs\Python\Python310\lib\site-packages\eth_tester\main.py", line 483, in estimate_gas
raw_gas_estimate = self.backend.estimate_gas(raw_transaction, raw_block_number)
TypeError: MockBackend.estimate_gas() takes 2 positional arguments but 3 were given
2) After fixing the first error by adding {'gas': 123456} in transact(), the second error takes place at greeter_obj.functions.greet().call() :
"ValueError: Error expected to be a dict."
For this one, I have no clue
Information�: impossible de trouver des fichiers pour le(s) mod�le(s) sp�cifi�(s).
C:\Users\Gilles\AppData\Local\Programs\Python\Python310\lib\site-packages\eth_tester\backends_init_.py:30: UserWarning: Ethereum Tester: No backend was explicitly set, and no full backends were available. Falling back to the MockBackend which does not support all EVM functionality. Please refer to the eth-tester documentation for information on what backends are available and how to set them. Your py-evm package may need to be updated.
warnings.warn(
{'blockHash': HexBytes('0xafae7675633fedae22a1f5b9d11066ff78de5947f7b3e2915824823cc65d0e56'),
'blockNumber': 1,
'contractAddress': '0xa0Beb7081fDaF3ed157370836A85eeC20CEc9e04',
'cumulativeGasUsed': 21000,
'effectiveGasPrice': 1000000000,
'from': '0xaBbACadABa000000000000000000000000000000',
'gasUsed': 21000,
'logs': [],
'state_root': b'\x00',
'status': 0,
'to': '',
'transactionHash': HexBytes('0x5193460ead56b33c2fa79b490a6c0f4e0d68e07c712d762afcadc5976148bf1a'),
'transactionIndex': 0,
'type': '0x2'}
Traceback (most recent call last):
File "D:_P\dev\python\blockchain\web3\tester1.py", line 54, in
print(f"{greeter_obj.functions.greet().call() = }")
File "C:\Users\Gilles\AppData\Local\Programs\Python\Python310\lib\site-packages\web3\contract.py", line 970, in call
return call_contract_function(
File "C:\Users\Gilles\AppData\Local\Programs\Python\Python310\lib\site-packages\web3\contract.py", line 1525, in call_contract_function
return_data = web3.eth.call(
File "C:\Users\Gilles\AppData\Local\Programs\Python\Python310\lib\site-packages\web3\module.py", line 57, in caller
result = w3.manager.request_blocking(method_str,
File "C:\Users\Gilles\AppData\Local\Programs\Python\Python310\lib\site-packages\web3\manager.py", line 198, in request_blocking
return self.formatted_response(response,
File "C:\Users\Gilles\AppData\Local\Programs\Python\Python310\lib\site-packages\web3\manager.py", line 170, in formatted_response
apply_error_formatters(error_formatters, response)
File "C:\Users\Gilles\AppData\Local\Programs\Python\Python310\lib\site-packages\web3\manager.py", line 70, in apply_error_formatters
formatted_resp = pipe(response, error_formatters)
File "cytoolz\functoolz.pyx", line 666, in cytoolz.functoolz.pipe
File "cytoolz\functoolz.pyx", line 641, in cytoolz.functoolz.c_pipe
File "C:\Users\Gilles\AppData\Local\Programs\Python\Python310\lib\site-packages\web3_utils\method_formatters.py", line 555, in raise_solidity_error_on_revert
raise ValueError('Error expected to be a dict')
ValueError: Error expected to be a dict
Please note the status of the deployment transaction: 'status': 0
It failed but a contractAddress was returned !
As far as the UserWarning is concerned, I also tried unsuccessfully MockBackend (though it's the default backend):
from eth_tester import MockBackend
w3 = Web3(Web3.EthereumTesterProvider(MockBackend()))
Lastly, I tried to install py-evm "pip install py-evm" to try PyEVMBackend backend, but the installation failed at pyethash dependency:
"D:\Program\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.34.31933\bin\HostX86\x64\cl.exe" /c /nologo /O2 /W3 /GL /DNDEBUG /MD -IC:\Users\Gilles\AppData\Local\Programs\Python\Python310\include -IC:\Users\Gilles\AppData\Local\Programs\Python\Python310\Include "-ID:\Program\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.34.31933\include" "-ID:\Program\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\VS\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22000.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\um" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\shared" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\winrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\cppwinrt" /Tcsrc/libethash/io_win32.c /Fobuild\temp.win-amd64-cpython-310\Release\src/libethash/io_win32.obj -Isrc/ -std=gnu99 -Wall
clÿ: Ligne de commande warning D9002ÿ: option '-std=gnu99' inconnue ignor‚e
io_win32.c
c1: fatal error C1083: Impossible d'ouvrir le fichier sourceÿ: 'src/libethash/io_win32.c'ÿ: No such file or directory
error: command 'D:\\Program\\Microsoft Visual Studio\\2022\\BuildTools\\VC\\Tools\\MSVC\\14.34.31933\\bin\\HostX86\\x64\\cl.exe' failed with exit code 2
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
error: legacy-install-failure
× Encountered error while trying to install package.
╰─> pyethash

Error:sqlalchemy.exc.InternalError: (pymysql.err.InternalError) Packet sequence number wrong - got 35 expected 1

This is my first question. Sorry if there is a mistake.
I am not good at English so there may be mistakes.
I am getting an error with the following information.
I don't know how to solve it. I would appreciate it if you could help me.
This error occurs if you do not access the API for several hours and then try to access the API after several hours.
This error can be recovered by restarting the FastAPI application.
Errorー
Traceback (most recent call last):
File "/home/ubuntu/app/backend/venv/lib/python3.8/site-packages/uvicorn/protocols/http/httptools_impl.py", line 375, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "/home/ubuntu/app/backend/venv/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 75, in __call__
return await self.app(scope, receive, send)
File "/home/ubuntu/app/backend/venv/lib/python3.8/site-packages/fastapi/applications.py", line 261, in __call__
await super().__call__(scope, receive, send)
File "/home/ubuntu/app/backend/venv/lib/python3.8/site-packages/starlette/applications.py", line 112, in __call__
await self.middleware_stack(scope, receive, send)
File "/home/ubuntu/app/backend/venv/lib/python3.8/site-packages/starlette/middleware/errors.py", line 181, in __call__
raise exc
File "/home/ubuntu/app/backend/venv/lib/python3.8/site-packages/starlette/middleware/errors.py", line 159, in __call__
await self.app(scope, receive, _send)
File "/home/ubuntu/app/backend/venv/lib/python3.8/site-packages/starlette/exceptions.py", line 82, in __call__
raise exc
File "/home/ubuntu/app/backend/venv/lib/python3.8/site-packages/starlette/exceptions.py", line 71, in __call__
await self.app(scope, receive, sender)
File "/home/ubuntu/app/backend/venv/lib/python3.8/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
raise e
File "/usr/lib/python3.8/contextlib.py", line 679, in __aexit__
raise exc_details[1]
File "/usr/lib/python3.8/contextlib.py", line 662, in __aexit__
cb_suppress = await cb(*exc_details)
File "/usr/lib/python3.8/contextlib.py", line 189, in __aexit__
await self.gen.athrow(typ, value, traceback)
File "/home/ubuntu/app/backend/db.py", line 41, in get_db
yield session
File "/home/ubuntu/app/backend/venv/lib/python3.8/site-packages/sqlalchemy/ext/asyncio/session.py", line 626, in __aexit__
await self.close()
File "/home/ubuntu/app/backend/venv/lib/python3.8/site-packages/sqlalchemy/ext/asyncio/session.py", line 608, in close
return await greenlet_spawn(self.sync_session.close)
File "/home/ubuntu/app/backend/venv/lib/python3.8/site-packages/sqlalchemy/util/_concurrency_py3k.py", line 134, in greenlet_spawn
result = context.throw(*sys.exc_info())
File "/home/ubuntu/app/backend/venv/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 1789, in close
self._close_impl(invalidate=False)
File "/home/ubuntu/app/backend/venv/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 1831, in _close_impl
transaction.close(invalidate)
File "/home/ubuntu/app/backend/venv/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 923, in close
transaction.close()
File "/home/ubuntu/app/backend/venv/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 2324, in close
self._do_close()
File "/home/ubuntu/app/backend/venv/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 2547, in _do_close
self._close_impl()
File "/home/ubuntu/app/backend/venv/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 2533, in _close_impl
self._connection_rollback_impl()
File "/home/ubuntu/app/backend/venv/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 2525, in _connection_rollback_impl
self.connection._rollback_impl()
File "/home/ubuntu/app/backend/venv/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 974, in _rollback_impl
self._handle_dbapi_exception(e, None, None, None, None)
File "/home/ubuntu/app/backend/venv/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 2032, in _handle_dbapi_exception
util.raise_(
File "/home/ubuntu/app/backend/venv/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
raise exception
File "/home/ubuntu/app/backend/venv/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 972, in _rollback_impl
self.engine.dialect.do_rollback(self.connection)
File "/home/ubuntu/app/backend/venv/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 682, in do_rollback
dbapi_connection.rollback()
File "/home/ubuntu/app/backend/venv/lib/python3.8/site-packages/sqlalchemy/dialects/mysql/aiomysql.py", line 205, in rollback
self.await_(self._connection.rollback())
File "/home/ubuntu/app/backend/venv/lib/python3.8/site-packages/sqlalchemy/util/_concurrency_py3k.py", line 76, in await_only
return current.driver.switch(awaitable)
File "/home/ubuntu/app/backend/venv/lib/python3.8/site-packages/sqlalchemy/util/_concurrency_py3k.py", line 129, in greenlet_spawn
value = await result
File "/home/ubuntu/app/backend/venv/lib/python3.8/site-packages/aiomysql/connection.py", line 358, in rollback
await self._read_ok_packet()
File "/home/ubuntu/app/backend/venv/lib/python3.8/site-packages/aiomysql/connection.py", line 331, in _read_ok_packet
pkt = await self._read_packet()
File "/home/ubuntu/app/backend/venv/lib/python3.8/site-packages/aiomysql/connection.py", line 572, in _read_packet
raise InternalError(
sqlalchemy.exc.InternalError: (pymysql.err.InternalError) Packet sequence number wrong - got 35 expected 1
(Background on this error at: https://sqlalche.me/e/14/2j85)
Development Environment
Python 3.8.10
mysql Ver 8.0.31-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))
Package Version
------------------- -----------
aiomysql 0.0.22
anyio 3.5.0
asgiref 3.5.0
autopep8 1.7.0
bcrypt 4.0.0
beautifulsoup4 4.11.1
certifi 2021.10.8
cffi 1.15.0
charset-normalizer 2.0.12
click 8.0.4
contourpy 1.0.5
cryptography 36.0.2
cycler 0.11.0
dnspython 2.2.1
ecdsa 0.18.0
email-validator 1.1.3
et-xmlfile 1.1.0
fastapi 0.75.0
fonttools 4.38.0
greenlet 1.1.2
gunicorn 20.1.0
h11 0.13.0
htmldocx 0.0.6
httptools 0.2.0
idna 3.3
itsdangerous 2.1.2
japanize-matplotlib 1.1.3
Jinja2 3.1.1
kiwisolver 1.4.4
lxml 4.9.1
MarkupSafe 2.1.1
matplotlib 3.6.1
numpy 1.23.4
openpyxl 3.0.10
orjson 3.6.7
packaging 21.3
passlib 1.7.4
Pillow 9.2.0
pip 20.0.2
pkg-resources 0.0.0
pyasn1 0.4.8
pycodestyle 2.9.1
pycparser 2.21
pydantic 1.9.0
PyMySQL 0.9.3
pyparsing 3.0.9
python-dateutil 2.8.2
python-docx 0.8.11
python-dotenv 0.20.0
python-jose 3.3.0
python-multipart 0.0.5
PyYAML 5.4.1
requests 2.27.1
rsa 4.9
setuptools 44.0.0
six 1.16.0
sniffio 1.2.0
soupsieve 2.3.2.post1
SQLAlchemy 1.4.32
starlette 0.17.1
toml 0.10.2
typing-extensions 4.1.1
ujson 4.3.0
urllib3 1.26.9
uvicorn 0.15.0
uvloop 0.16.0
watchgod 0.8.1
websockets 10.2
wheel 0.37.1
Source Code
app.py
from fastapi import FastAPI
from routers.v1 import XXXXXXX, XXXXXXX
from core.handlers import api_exception_handler, system_exception_handler
from exceptions import ApiException
app = FastAPI()
app.add_exception_handler(ApiException, api_exception_handler)
app.add_exception_handler(Exception, system_exception_handler)
prefix="/api/v1"
app.include_router(XXXX.router, prefix=prefix, tags=['XXXX'])
app.include_router(XXXX.router, prefix=prefix, tags=['XXXX'])
db.py
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from sqlalchemy.orm import sessionmaker, declarative_base
import core.config as config
DB_USER = config.get_env().db_user
DB_PASSWORD = config.get_env().db_password
DB_HOST = config.get_env().db_host
DB_NAME = config.get_env().db_name
DB_CONNETION_STRING = 'mysql+aiomysql://%s:%s#%s/%s?charset=utf8mb4' % (
DB_USER,
DB_PASSWORD,
DB_HOST,
DB_NAME,
)
async_engine = create_async_engine(
DB_CONNETION_STRING,
echo=True
)
async_session = sessionmaker(
autocommit=False,
autoflush=False,
bind=async_engine,
class_=AsyncSession
)
Base = declarative_base()
async def get_db():
async with async_session() as session:
yield session
I searched for the error and tried multiple suggestions, but could not resolve the issue.
I am not very familiar with Python, so I may have done something wrong.

GCloud function call error: Error: could not handle the request

I have billing enabled for my project, however, I keep on getting an error returned:
Error: could not handle the request
And the traceback:
Traceback (most recent call last):
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/requests/models.py", line 971, in json
return complexjson.loads(self.text, **kwargs)
File "/opt/python3.9/lib/python3.9/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "/opt/python3.9/lib/python3.9/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/opt/python3.9/lib/python3.9/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Here is my code:
import json
from google.cloud import storage
import requests
def call(request):
url = "https://jsonplaceholder.typicode.com/todos/"
response = requests.get(url)
json_data = response.json()
pretty_json = json.dumps(json_data)
print(pretty_json)
With requirements being:
requests
google.cloud.storage
The following should work for you:
Create a virtualenv
python3 -m venv venv
source venv/bin/activate
Install Requests (you're not using Google Cloud Storage)
python3 -m pip install requests
Script
import json
import requests
url = "https://jsonplaceholder.typicode.com/todos/"
response = requests.get(url)
print(response.status_code)
print(response.text)
Execute the code
python3 main.py
Results
200
[
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
},
...
]

Unable to send json request when using fastapi

I have error in the following code when I try to send a request with json data in it, is it because it only works synchronously, so does not work in fastapi which work asynchronously? If that is the case, what is the simplest way to send a request with json data in it?
import time
from fastapi import Request, FastAPI, BackgroundTasks
import multiprocessing as mp
import uvicorn
import requests
import json
def printmessage(job):
time.sleep(5)
print(job)
if __name__ == 'webhook_fastapi':
url = 'http://127.0.0.1:8000/webhook'
data = { 'text': 'hello'}
r = requests.post(url, data=json.dumps(data), headers={'Content-Type': 'application/json'})
print("Request Sent!")
app = FastAPI()
#app.post("/webhook")
async def webhook(request : Request, background_tasks: BackgroundTasks):
print("WEBHOOK RECEIVED")
job="doctor"
background_tasks.add_task(printmessage,job)
print('done')
return 'WEBHOOK RECEIVED'
if __name__ == '__main__':
print("PROGRAM LAUNCH...")
print("WEBHOOK RECEIVE READY...")
background_tasks = BackgroundTasks()
job="doctor"
background_tasks.add_task(printmessage,job)
uvicorn.run("webhook_fastapi:app", reload=False)
OUTPUT:
__main__
PROGRAM LAUNCH...
WEBHOOK RECEIVE READY...
webhook_fastapi
Traceback (most recent call last): File
"C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connection.py",
line 169, in _new_conn
conn = connection.create_connection( File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\util\connection.py",
line 96, in create_connection
raise err File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\util\connection.py",
line 86, 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\User\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py",
line 699, in urlopen
httplib_response = self._make_request( File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py",
line 394, in _make_request
conn.request(method, url, **httplib_request_kw) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connection.py",
line 234, in request
super(HTTPConnection, self).request(method, url, body=body, headers=headers) File
"C:\Users\User\AppData\Local\Programs\Python\Python39\lib\http\client.py",
line 1279, in request
self._send_request(method, url, body, headers, encode_chunked) File
"C:\Users\User\AppData\Local\Programs\Python\Python39\lib\http\client.py",
line 1325, in _send_request
self.endheaders(body, encode_chunked=encode_chunked) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\http\client.py",
line 1274, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked) File
"C:\Users\User\AppData\Local\Programs\Python\Python39\lib\http\client.py",
line 1034, in _send_output
self.send(msg) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\http\client.py",
line 974, in send
self.connect() File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connection.py",
line 200, in connect
conn = self._new_conn() File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connection.py",
line 181, in _new_conn
raise NewConnectionError( urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x0000013C0E494310>:
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\User\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\adapters.py",
line 439, in send
resp = conn.urlopen( File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py",
line 755, in urlopen
retries = retries.increment( File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\util\retry.py",
line 574, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1',
port=8000): Max retries exceeded with url: /webhook (Caused by
NewConnectionError('<urllib3.connection.HTTPConnection object at
0x0000013C0E494310>: 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*\webhook_fastapi.py", line 40, in
uvicorn.run("webhook_fastapi:app", reload=False) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\uvicorn\main.py",
line 463, in run
server.run() File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\uvicorn\server.py",
line 60, in run
return asyncio.run(self.serve(sockets=sockets)) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\asyncio\runners.py",
line 44, in run
return loop.run_until_complete(main) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py",
line 642, in run_until_complete
return future.result() File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\uvicorn\server.py",
line 67, in serve
config.load() File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\uvicorn\config.py",
line 458, in load
self.loaded_app = import_from_string(self.app) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\uvicorn\importer.py",
line 21, in import_from_string
module = importlib.import_module(module_str) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\importlib_init_.py",
line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level) File "", line 1030, in _gcd_import File
"", line 1007, in _find_and_load File
"", line 986, in _find_and_load_unlocked
File "", line 680, in _load_unlocked
File "", line 850, in
exec_module File "", line 228, in
_call_with_frames_removed File "c:\Users*\webhook_fastapi.py", line 19, in
r = requests.post(url, data=json.dumps(data), headers={'Content-Type': 'application/json'}) File
"C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\api.py",
line 117, in post
return request('post', url, data=data, json=json, **kwargs) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\api.py",
line 61, in request
return session.request(method=method, url=url, **kwargs) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py",
line 542, in request
resp = self.send(prep, **send_kwargs) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py",
line 655, in send
r = adapter.send(request, kwargs) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\adapters.py",
line 516, in send
raise ConnectionError(e, request=request) requests.exceptions.ConnectionError:
HTTPConnectionPool(host='127.0.0.1', port=8000): Max retries exceeded
with url: /webhook (Caused by
NewConnectionError('<urllib3.connection.HTTPConnection object at
0x0000013C0E494310>: Failed to establish a new connection: [WinError
10061] No connection could be made because the target machine actively
refused it')) PS C:\Users*>
There is no server running when you're trying to make your request. uvicorn is importing your code, and you're apparently trying to make a request while the code is being imported.
uvicorn can't launch the API and bind to the port before it has finished importing the code and the ASGI application has been set up and properly configured.

Is there a limit on input json string for aws sagemaker endpoint?

I have ~5MB json string that I want to send to my endpoint. I am using boto3.client to invoke the endpoint from my python client. It throws ConnectionResetError.
File "C:\Users\corona\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connectionpool.py", line 600, in urlopen
chunked=chunked)
File "C:\Users\corona\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connectionpool.py", line 354, in _make_request
conn.request(method, url, **httplib_request_kw)
File "C:\Users\corona\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1229, in request
self._send_request(method, url, body, headers, encode_chunked)
File "C:\Users\corona\AppData\Local\Programs\Python\Python37\lib\site-packages\botocore\awsrequest.py", line 92, in _send_request
method, url, body, headers, *args, **kwargs)
File "C:\Users\corona\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1275, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "C:\Users\corona\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1224, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "C:\Users\corona\AppData\Local\Programs\Python\Python37\lib\site-packages\botocore\awsrequest.py", line 119, in _send_output
self.send(msg)
File "C:\Users\corona\AppData\Local\Programs\Python\Python37\lib\site-packages\botocore\awsrequest.py", line 203, in send
return super(AWSConnection, self).send(str)
File "C:\Users\corona\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 977, in send
self.sock.sendall(data)
File "C:\Users\corona\AppData\Local\Programs\Python\Python37\lib\ssl.py", line 1012, in sendall
v = self.send(byte_view[count:])
File "C:\Users\corona\AppData\Local\Programs\Python\Python37\lib\ssl.py", line 981, in send
return self._sslobj.write(data)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
Looking at the trace, I am guessing it is due to json string size. Could someone please help me how to get around this?
Exceeding the payload size limit does result in a connection reset from the SageMaker Runtime service.
From the SageMaker documentation:
Maximum payload size for endpoint invocation | 5 MB
There are likely more space-efficient data formats than JSON that you could use to transmit the payload, but the available options will depend on the type of data and what model image you are using (i.e. whether Amazon-provided or a custom implementation).