8000 File uploading support by so-saf · Pull Request #597 · ctalkington/python-ipp · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

File uploading support #597

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
35 changes: 35 additions & 0 deletions examples/add_modify_printer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# pylint: disable=W0621
"""Asynchronous Python client for IPP."""
import asyncio
from pathlib import Path

from pyipp import IPP
from pyipp.enums import IppOperation


async def main() -> None:
"""Show an example of add or modifying printer on your IP print server."""
content = Path("/path/to/driver.ppd").read_bytes()

async with IPP(
host="ipp://127.0.0.1:631/printers/My_New_Printer",
username="",
password="",
) as ipp:
response = await ipp.execute(
IppOperation.CUPS_ADD_MODIFY_PRINTER,
{
"printer-attributes-tag": {
"device-uri": "socket://192.168.0.12:9100",
"printer-info": "My awesome printer",
"printer-location": "office",
},
"file": content,
},
)

print(response)


if __name__ == "__main__":
asyncio.run(main())
11 changes: 5 additions & 6 deletions examples/print_example.py → examples/print_job.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# pylint: disable=W0621
"""Asynchronous Python client for IPP."""
import asyncio
from pathlib import Path

from pyipp import IPP
from pyipp.enums import IppOperation


async def main() -> None:
"""Show an example of printing on your IP print server."""
content = Path("/path/to/pdf.pdf").read_bytes()

pdf_file = '/path/to/pdf.pfd'
with open(pdf_file, 'rb') as f:
content = f.read()

"""Show example of executing operation against your IPP print server."""
# then the printer must be shared if CUPS is used
async with IPP("ipp://192.168.1.92:631/ipp/print") as ipp:
response = await ipp.execute(
IppOperation.PRINT_JOB,
Expand All @@ -22,7 +21,7 @@ async def main() -> None:
"job-name": "My Test Job",
"document-format": "application/pdf",
},
'data': content,
"file": content,
},
)

Expand Down
4 changes: 2 additions & 2 deletions src/pyipp/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def encode_dict(data: dict[str, Any]) -> bytes:

encoded += struct.pack(">b", IppTag.END.value)

if "data" in data:
encoded += data["data"]
if isinstance(data.get("file"), bytes):
encoded += data["file"]

return encoded
Binary file not shown.
25 changes: 25 additions & 0 deletions tests/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,28 @@ def test_encode_dict() -> None:
assert result == load_fixture_binary(
"serializer/get-printer-attributes-request-000.bin",
)


def test_encode_dict_with_file() -> None:
"""Test the encode_dict method with upload file."""
result = serializer.encode_dict(
{
"version": DEFAULT_PROTO_VERSION,
"operation": IppOperation.CUPS_ADD_MODIFY_PRINTER,
"request-id": 1,
"operation-attributes-tag": {
"attributes-charset": DEFAULT_CHARSET,
"attributes-natural-language": DEFAULT_CHARSET_LANGUAGE,
"printer-uri": "ipp://printer.example.com:632/printers/My_New_Printer",
"requesting-user-name": "PythonIPP",
},
"printer-attributes-tag": {
"device-uri": "socket://0.0.0.0:9100",
},
"file": b"*PPD-Adobe:...",
},
)

assert result == load_fixture_binary(
"serializer/add-printer-with-driver-file-request-000.bin",
)
0