-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan_and_save.py
More file actions
62 lines (55 loc) · 2.22 KB
/
scan_and_save.py
File metadata and controls
62 lines (55 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import sys
import time
import requests
if len(sys.argv) != 2:
print(f"Usage: {sys.argv[0]} <ip:port>")
sys.exit(1)
ip_port = sys.argv[1]
scanner_url = f"http://{ip_port}/eSCL/ScanJobs"
# https://github.com/alexpevzner/eSCL-protocol-traces/blob/master/Kyocera-ECOSYS-M2040dn.log
scan_job_xml = """
<?xml version="1.0" encoding="UTF-8"?>
<scan:ScanSettings xmlns:pwg="http://www.pwg.org/schemas/2010/12/sm" xmlns:scan="http://schemas.hp.com/imaging/escl/2011/05/03">
<pwg:Version>2.0</pwg:Version>
<pwg:ScanRegions>
<pwg:ScanRegion>
<pwg:ContentRegionUnits>escl:ThreeHundredthsOfInches</pwg:ContentRegionUnits>
<pwg:XOffset>0</pwg:XOffset>
<pwg:YOffset>0</pwg:YOffset>
<pwg:Width>2551</pwg:Width>
<pwg:Height>3508</pwg:Height>
</pwg:ScanRegion>
</pwg:ScanRegions>
<pwg:InputSource>Platen</pwg:InputSource>
<scan:ColorMode>RGB24</scan:ColorMode>
<pwg:DocumentFormat>image/jpeg</pwg:DocumentFormat>
<scan:XResolution>300</scan:XResolution>
<scan:YResolution>300</scan:YResolution>
</scan:ScanSettings>
"""
headers = {"Content-Type": "text/xml"}
response = requests.post(scanner_url, data=scan_job_xml.encode("utf-8"), headers=headers)
if response.status_code == 201:
print("Scan job created successfully.")
print("waiting for the scan to complete...")
time.sleep(5) # Wait for the scan job to complete
print("Downloading scanned image...")
scan_job_location = response.headers.get("Location")
if scan_job_location:
image_response = requests.get(scan_job_location+"/NextDocument")
if image_response.status_code == 200:
filename = f"scanned_image_{ip_port}_{int(time.time())}.jpg"
with open(filename, "wb") as f:
f.write(image_response.content)
print(f"Scanned image saved as {filename}")
else:
print("Failed to download scanned image.")
print("Status:", image_response.status_code)
print("Response:", image_response.text)
else:
print("Failed to download scanned image.")
print("No Location header found in response.")
else:
print("Failed to create scan job.")
print("Status:", response.status_code)
print("Response:", response.text)