Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #513 pkasyncio protect getpeername better #515

Merged
merged 2 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions pykern/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,10 @@ def __init__(self, http_config):
)
self._tornado = tornado.httpclient.AsyncHTTPClient(force_instance=True)

async def post(self, api_name, api_arg):
async def post(self, api_name, api_args):
r = await self._tornado.fetch(
self._uri,
body=_pack_msg(PKDict(api_name=api_name, api_arg=api_arg)),
body=_pack_msg(PKDict(api_name=api_name, api_args=api_args)),
headers=self._headers,
method="POST",
)
Expand All @@ -160,7 +160,7 @@ async def post(self, api_name, api_arg):
raise InvalidResponse(*e)
if rv.api_error:
raise APIError(
"api_error={} api_name={} api_arg={}", rv.api_error, api_name, api_arg
"api_error={} api_name={} api_args={}", rv.api_error, api_name, api_args
)
return rv.api_result

Expand Down Expand Up @@ -212,9 +212,9 @@ def _api_map():
loop.http_server(h)

async def dispatch(self, handler):
async def _call(api, api_arg):
async def _call(api, api_args):
with pykern.quest.start(api.api_class, self.attr_classes) as qcall:
return await getattr(qcall, api.api_func_name)(api_arg)
return await getattr(qcall, api.api_func_name)(api_args)

m = None
try:
Expand All @@ -226,7 +226,7 @@ async def _call(api, api_arg):
raise Forbidden(*e)
if not (a := self.api_map.get(m.api_name)):
raise NotFound("unknown api={}", m.api_name)
r = Reply(result=await _call(a, m.api_arg))
r = Reply(result=await _call(a, m.api_args))
except APIError as e:
r = Reply(api_error=e.pk_args.api_error)
except Exception as e:
Expand Down
6 changes: 4 additions & 2 deletions pykern/pkasyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ def _remote_peer(request):
# implementation may change; Code in tornado.httputil check connection.
if c := request.connection:
# socket is not set on stream for websockets.
if hasattr(c, "stream") and hasattr(c.stream, "socket"):
return "{}:{}".format(*c.stream.socket.getpeername())
if getattr(c, "stream", None) and (
s := getattr(c.stream, "socket", None)
):
return "{}:{}".format(*s.getpeername())
i = request.headers.get("proxy-for", request.remote_ip)
return f"{i}:0"

Expand Down
13 changes: 13 additions & 0 deletions pykern/pkdebug.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,19 @@ def pkdp(fmt_or_arg, *args, **kwargs):
pkdlog = pkdp


def pkdppretty(obj):
"""Print `obj` using `pkdpretty` and return `obj`

Args:
obj (object): JSON string or python object

Returns:
object: `obj`
"""
pkdp("{}", pkdpretty(obj))
return obj


def pkdpretty(obj):
"""Return pretty print the object.

Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,8 @@ pykern = "pykern.pykern_console:main"
[project.urls]
Homepage = "http://pykern.org"

[tool.setuptools.packages.find]
include = ["pykern*"]

[tool.setuptools.package-data]
pykern = ["package_data/**"]