1 | #!/usr/bin/python |
---|
2 | |
---|
3 | import logging, BaseHTTPServer |
---|
4 | import ipp |
---|
5 | import ipp.constants as const |
---|
6 | |
---|
7 | logging.basicConfig(level=logging.DEBUG) |
---|
8 | |
---|
9 | class GutenbachIPPHandler(BaseHTTPServer.BaseHTTPRequestHandler): |
---|
10 | def handle_one_request(self): |
---|
11 | self.raw_requestline = self.rfile.readline() |
---|
12 | if not self.raw_requestline: |
---|
13 | self.close_connection = 1 |
---|
14 | return |
---|
15 | if not self.parse_request(): # An error code has been sent, just exit |
---|
16 | return |
---|
17 | self.handle_ipp() |
---|
18 | |
---|
19 | def handle_ipp(self): |
---|
20 | length = int(self.headers.getheader('content-length', 0)) |
---|
21 | request = ipp.Request(request=self.rfile, |
---|
22 | length=length) |
---|
23 | |
---|
24 | response_kwargs = {} |
---|
25 | response_kwargs['version'] = request.version |
---|
26 | response_kwargs['request_id'] = request.request_id |
---|
27 | response_kwargs = self.get_jobs(request, response_kwargs) |
---|
28 | response = ipp.Request(**response_kwargs) |
---|
29 | |
---|
30 | self.send_response(200, "o hai") |
---|
31 | self.send_header("Content-Type", "application/ipp") |
---|
32 | self.send_header("Connection", "close") |
---|
33 | self.end_headers() |
---|
34 | self.wfile.write(response.packed_value) |
---|
35 | |
---|
36 | def get_jobs(self, request, response_kwargs): |
---|
37 | """get-jobs response""" |
---|
38 | |
---|
39 | job_attributes = [ipp.Attribute('job-id', |
---|
40 | [ipp.Value(ipp.Tags.INTEGER, |
---|
41 | 12345, |
---|
42 | )]), |
---|
43 | ipp.Attribute('job-name', |
---|
44 | [ipp.Value(ipp.Tags.NAME_WITHOUT_LANGUAGE, |
---|
45 | 'foo', |
---|
46 | )]), |
---|
47 | ipp.Attribute('job-originating-user-name', |
---|
48 | [ipp.Value(ipp.Tags.NAME_WITHOUT_LANGUAGE, |
---|
49 | 'jhamrick', |
---|
50 | )]), |
---|
51 | ipp.Attribute('job-k-octets', |
---|
52 | [ipp.Value(ipp.Tags.INTEGER, |
---|
53 | 1, |
---|
54 | )]), |
---|
55 | ipp.Attribute('job-state', |
---|
56 | [ipp.Value(ipp.Tags.ENUM, |
---|
57 | const.JobStates.HELD, |
---|
58 | )]), |
---|
59 | ipp.Attribute('job-printer-uri', |
---|
60 | [ipp.Value(ipp.Tags.URI, |
---|
61 | 'http://localhost:8000/printers/foo', |
---|
62 | )])] |
---|
63 | |
---|
64 | |
---|
65 | #req_op_attributes = request.getAttributeGroup(ipp.Tags.OPERATION_ATTRIBUTES_TAG) |
---|
66 | #print req_op_attributes |
---|
67 | #printer_uri = req_op_attributes[0].getAttribute('printer-uri') |
---|
68 | |
---|
69 | op_attributes = [ipp.Attribute('attributes-charset', |
---|
70 | [ipp.Value(ipp.Tags.CHARSET, |
---|
71 | 'utf-8', |
---|
72 | )]), |
---|
73 | ipp.Attribute('attributes-natural-language', |
---|
74 | [ipp.Value(ipp.Tags.NATURAL_LANGUAGE, |
---|
75 | 'en-us', |
---|
76 | )])] |
---|
77 | |
---|
78 | job_attribute_group = ipp.AttributeGroup(const.AttributeTags.JOB, |
---|
79 | job_attributes) |
---|
80 | op_attributes_group = ipp.AttributeGroup(const.AttributeTags.OPERATION, |
---|
81 | op_attributes) |
---|
82 | response_kwargs['attribute_groups'] = [op_attributes_group,job_attribute_group] |
---|
83 | response_kwargs['operation_id'] = const.StatusCodes.OK |
---|
84 | |
---|
85 | return response_kwargs |
---|
86 | |
---|
87 | ##### Printer Commands |
---|
88 | |
---|
89 | def print_job(self, request): |
---|
90 | pass |
---|
91 | |
---|
92 | def validate_job(self, request): |
---|
93 | pass |
---|
94 | |
---|
95 | def get_printer_attributes(self, request): |
---|
96 | pass |
---|
97 | |
---|
98 | #def get_jobs(self, request): |
---|
99 | # pass |
---|
100 | |
---|
101 | def print_uri(self, request): |
---|
102 | pass |
---|
103 | |
---|
104 | def create_job(self, request): |
---|
105 | pass |
---|
106 | |
---|
107 | def cups_get_default(self, request): |
---|
108 | pass |
---|
109 | |
---|
110 | def cups_get_printers(self, request): |
---|
111 | pass |
---|
112 | |
---|
113 | def pause_printer(self, request): |
---|
114 | pass |
---|
115 | |
---|
116 | def resume_printer(self, request): |
---|
117 | pass |
---|
118 | |
---|
119 | def set_printer_attributes(self, request): |
---|
120 | pass |
---|
121 | |
---|
122 | ##### Job Commands |
---|
123 | |
---|
124 | def cancel_job(self, request): |
---|
125 | pass |
---|
126 | |
---|
127 | def get_job_attributes(self, request): |
---|
128 | pass |
---|
129 | |
---|
130 | def send_document(self, request): |
---|
131 | pass |
---|
132 | |
---|
133 | def send_uri(self, request): |
---|
134 | pass |
---|
135 | |
---|
136 | def set_job_attributes(self, request): |
---|
137 | pass |
---|
138 | |
---|
139 | def cups_get_document(self, request): |
---|
140 | pass |
---|
141 | |
---|
142 | def restart_job(self, request): |
---|
143 | pass |
---|
144 | |
---|
145 | def promote_job(self, request): |
---|
146 | pass |
---|
147 | |
---|
148 | if __name__ == '__main__': |
---|
149 | server_address = ('', 8000) |
---|
150 | httpd = BaseHTTPServer.HTTPServer(server_address, GutenbachIPPHandler) |
---|
151 | httpd.serve_forever() |
---|