source: server/lib/server.py @ dc37ed0

no-cups
Last change on this file since dc37ed0 was dc37ed0, checked in by Quentin Smith <quentin@…>, 13 years ago

Show request and response for debugging purposes

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