Posts Tagged ‘code’

Transport class for Python's XML-RPC lib

Thursday, June 21st, 2007

Given that the xmlrpclib.Transport class can be derived, it is perhaps easier to define a new transport class that implements the patch shown in the facelift post, though I still believe Python’s XML-RPC library is due a much needed update.

Thus, I present the HTTPTransport class:

Update: It seems I forgot to parse the resulting payload. this is now fixed in the updated code below.

from xmlrpclib import Transport
from xmlrpclib import ProtocolError

class HTTPTransport(Transport):
    ##
    # Connect to server.
    #
    # @param host Target host.
    # @return A connection handle.

    def make_connection(self, host):
        # create a HTTP connection object from a host descriptor
        import httplib
        host, extra_headers, x509 = self.get_host_info(host)
        return httplib.HTTPConnection(host)
    ##
    # Send a complete request, and parse the response.
    #
    # @param host Target host.
    # @param handler Target PRC handler.
    # @param request_body XML-RPC request body.
    # @param verbose Debugging flag.
    # @return XML response.

    def request(self, host, handler, request_body, verbose=0):
        # issue XML-RPC request

        h = self.make_connection(host)
        if verbose:
            h.set_debuglevel(1)

        self.send_request(h, handler, request_body)
        self.send_host(h, host)
        self.send_user_agent(h)
        self.send_content(h, request_body)

        response = h.getresponse()

        if response.status != 200:
          raise ProtocolError(host + handler, response.status, response.reason, response.msg.headers)

        payload = response.read()
        parser, unmarshaller = self.getparser()
        parser.feed(payload)
        parser.close()

        return unmarshaller.close()

On the joys of C

Friday, December 1st, 2006

Half a year ago, I posted about the lack of programming language knowledge the CS students have, and which they require for courses besides the actual programming courses. Yesterday I was assisting at a practicum on operating systems. The assignment was quite easy: add some code to get a working linux module that allows mounting and reading a tarball. To my horror, the following snippets were jotted down in the students favourite editor (pico). FYI, bd is a char*, and points to the header of the tarball. Find the errors. Pick your favourite one.

  String name;

  char *tmp;
  strncpy(tmp, bd[124], 12);

  bd.substring(124,136);

  printk("size = " + this_tar->fileinfo[next_inode].size)

And, oh joy! Who needs compiler warnings or errors anyway? Crap spouted to the screen can be safely ignored until we get a kernel oops, or better, a automatic reboot.

We really, really need to teach these kids something besides Java. There is supposedly a C course compiled by students that they very urgently need to read!

Programming Languages or Lack Thereof

Thursday, March 9th, 2006

The first programming languages I was taught were (in order): (Turbo) Pascal, C and C++. During my final year at uni I grew an interest in languages no professor ever spoke of: Perl, Python, Awk, Bash (as far as shell stuff can be called a language). I took a course on functional and logical programming – for which I happen to be one of the teaching assistants at the moment – where I met Haskell and Prolog. While the latter has never managed to grab my interest, the former I really took a liking to. The functional programming course got me looking at Clean, Ocaml, SML, … I must admit I never really got used to the ML’s, but that’s largely because I do not digg the syntax very much.

Somehow it seems a bit sad that there are no real introductions or even hack sessions for the alternative (in the mainstream opinion) languages such as Python, Perl or even Ocaml or Clean. Even PHP is skipped, AFAIK. There are some introductions given by students to students, but it would be better if there was at least some structured course that would introduce at least a few of these languages, say Python, PHP and Haskell (the functional programming course is dying with the new Bachelor/Master system).

I think that most programming languages can contribute something essential to the programmers toolbox. (Even Visual Basic. Like how not to do it.) However, the last years the focus of the Computer Science courses has been more and more committed to Java. Java for starters, Java for Datastructures and Algorithms, Java for this, Java for that. I am the first to admit that Java has its uses, and that when used in the right environment it’s even quite cool, but why drop everything else in favour of Java? Because the industry demands it? Should the university not deliver computer scientists with a broad spectrum of academic qualities and with a broad range of programming languages they at least know the basics of? Of course they should be proficient in one or two languages, but they should at least have some knowledge of others. At least they should know what language is best suited for a particular task. Java is not a text-processing language, whereas Perl is. Right now we seem to deliver Java bees. Who can explain to these people what a pointer is? When the next mainstream, overhyped language rolls along, will we teach only that language?

Maybe we should teach people some brainfuck, whitespace and friends.