Can anyone please analyse this program for me (write a pseudocode for it).
Tim Chase
python.list at tim.thechases.com
Mon Jul 3 10:12:56 EDT 2006
> /* $Id: dotquad.c 3529 2005-10-01 10:15:22Z dyoung $ */
Well, let's begin here. You've got your python commenting style
all wrong. This alone won't parse as python. I recommend using
the standard "#" comment notation as described in the python docs.
> if (argc != 2 || !inet_aton(argv[1], &addr)) {
> usage(argv[0]);
> }
>
> (void)printf("%s\n", inet_ntoa(addr));
Then, in your python code, you're using braces. While there are
conventions for doing that:
if (conditions): # {
statements
#}
they clearly aren't being followed in your code, and given their
optional nature, I suggest simply omitting them. Second of all,
you're calling non-idempotent functions in your "if" statement.
Generally considered bad style. Additionally, you never
reference the sys.argv module/list of parameters, but still refer
to argc (which should really be "len(sys.argv)"), and
The lines that look like they should be including your modules:
> #include <err.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <sys/socket.h>
> #include <netinet/in.h>
> #include <arpa/inet.h>
appear to be commented out, as per above, using the standard "#"
notation. Additionally, the python syntax would be "import
socket", not "include" followed by all this other rubbish with
less-than and greater-than signs, and filename extensions.
This isn't rocket-surgery. Both inet_aton() and inet_ntoa()
function calls are in the socket module. Call them accordingly:
>>> import socket
>>> print "\n".join([s for s in dir(socket) if
s.startswith("inet_")])
inet_aton
inet_ntoa
>>> help(socket.inet_aton)
Help on built-in function inet_aton in module _socket:
inet_aton(...)
inet_aton(string) -> packed 32-bit IP representation
Convert an IP address in string format (123.45.67.89) to the
32-bit packed
binary format used in low-level network functions.
>>> help(socket.inet_ntoa)
Help on built-in function inet_ntoa in module _socket:
inet_ntoa(...)
inet_ntoa(packed_ip) -> ip_address_string
Convert an IP address from 32-bit packed binary format to
string format
>>> a = s.inet_aton("192.168.3.14")
>>> a
'\xc0\xa9\x03\x0e'
>>> s.inet_ntoa(a)
'192.168.3.14'
The rest is basic candy to get the command-line parameter and
pass it in to aton and back again to ntoa.
I'm sure there are plenty of other problems, but this would be a
good place to start...
-tkc
More information about the Python-list
mailing list