IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New But not as readable as Python.
perl isn't a beginner's language, IMO.
Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
New This isn't readable?
\n#!/usr/bin/perl -w\nuse strict;\n\nuse IO::Socket qw(:DEFAULT :crlf);\n\nmy $host = shift;\nmy $port = shift;\nmy $string = shift;\nunless (defined($string)){\n        die "Must supply host, port, and text string to send!\\n";\n}\n\nprint "[$host]:[$port]:[$string]\\n";\n\nmy $sock = IO::Socket::INET->new("$host:$port")\n        or die "Can't connect to $host:$port: $!\\n";\n\n\nmy($rc);\n$rc = printf $sock ("$string\\n");\nif (!$rc) {\n        printf STDERR ("Can't write, client connection error %s", $sock->error());\n        $sock->close();\n        exit(1);\n}\nmy($line);\nif (!defined($line = $sock->getline())) {\n        printf STDERR ("Can't read, client connection error %s", $sock->error());\n        $sock->close();\n        exit(1);\n}\n$sock->close();\n\nprint $line;\nexit(0);\n
New Ben could reduce that to about 12 characters... ;-)
New Of course he could
And we'd all ooooh and aaaah.

And then 2 years from now, the junior maintainance programmer would hunt him down and kill him.

New :-) Yup, but in Python one can't do that.
Apparent line noise isn't significant information in Python.

Cheers,
Scott.
New Hmm
Before this degenerates into a language war, something is missing.

COMPETING CODE!

Until posted, the evangelists of nothing to say about it.
New Write That Code!
Contestant: "Tom, I can Write That Code in 15 Characters!"

Audience:Whooo!

Tom Kennedy: "Write That Code!"

:-D

Cheers,
Scott.
New Sorry I'm late to the Party ... Ruby Version
As Ben suggested, Ruby is not a bad choice either.
\n#!/bin/env ruby\n\nrequire 'socket'\n\nhost, port, string = ARGV\nfail "Must supply host, port, and text string to send!" unless string\n\nputs "#{host}:#{port}:#{string}"\n\nTCPSocket.open(host, port) do |sock|\n  sock.puts(string)\n  puts sock.gets\nend\n
And the low level protocols are there too, if you wish to work at the bind/connect level.
--
-- Jim Weirich jim@weirichhouse.org [link|http://onestepback.org|http://onestepback.org]
---------------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)
New Where's the error handling?
For the curious... and since the other examples had it. :-)
Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
New I wouldn't bet that it is missing
I don't know that library, but a common Ruby idiom is that if you pass a block to the open method, then the called method is responsible for doing the open, loop, and necessary error checks. :-)

Cheers,
Ben
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
New OK, fine... but
Where is the error handling? :-)

Unless the library is just eating the error (bad), then the code is ignoring the error (which is what I'm asking about).

In other words, how will it know if the connection fails?
Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
New As I said...
If it follows the idiom that I've seen in other libraries, the library throws an exception that will display default error messages which will suffice for simple scripts.

Cheers,
Ben
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
New Near enough to Python, then.
But for purposes of this example, that's not what's being done.
Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
New Re: Near enough to Python, then.
But for purposes of this example, that's not what's being done.

Actually, it is. The Perl and Python examples (1) close the socket (if it is open), (2) print a simple error message, (3) and exit with an error code.

The Ruby version handles (1) within the open method automatically, and (2) and (3) in the top level exception handler provided by Ruby.

If you want a more custom error message than is provided by the default error handler, or if you want a more specific error code on exit, you could do:
\nbegin\n  TCPSocket.open(host, port) do |sock|\n    sock.puts(string)\n    puts sock.gets\n  end\nrescue Exception => ex\n  puts "Error: #{ex} at #{ex.backtrace[0]}"\n  exit(1)\nend\n
Note that it is not necessary to explicitly close the socket, even in the presence of exceptions, when using the block form of open as coded in the examples.

Error output from original code:
\nrnet.rb:10:in `initialize': Connection refused - connect(2) (Errno::ECONNREFUSED)\n\tfrom rnet.rb:10:in `open'\n\tfrom rnet.rb:10\n
Error output from the modified code above:
\nError: Connection refused - connect(2) at rnet2.rb:11:in `initialize'\n
--
-- Jim Weirich jim@weirichhouse.org [link|http://onestepback.org|http://onestepback.org]
---------------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)
New As I said...
The Python example does pretty much the same thing.

If you leave off the exception handling, it closes the socket, prints a simple error message, and exits with an error code. There's nothing magical in the Ruby example. :-)

So again, the examples being given here DO have a more custom error message. Which was all I was asking for. The point of the examples being good, explicit (as in, you can expect errors here, etc.) networking code suitable for showing to a beginner.

So, thanks for the example. :-)
Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
Expand Edited by admin Sept. 19, 2005, 08:53:23 PM EDT
New Ahh, makes me long for the old days
NOT!

Doing IO programming (serial or parallel), bit twiddling the IOCTL calls for every byte, checking the handshake lines, etc.

Man that sucked!
New Oops.
Expand Edited by inthane-chan Sept. 15, 2005, 06:47:23 PM EDT
New ICJRLPD (new thread)
Created as new thread #224882 titled [link|/forums/render/content/show?contentid=224882|ICJRLPD]
When somebody asks you to trade your security for freedom, it isn't your freedom they're talking about.
New As I said, "not AS readable as the Python"
Perl is a difficult language for newbies.
#!/usr/bin/python\n\nimport sys, socket, telnetlib\n\nif (len(sys.argv) < 4):\n    sys.exit('Must supply host, port, and text string to send!\\n')\n\n(host, port, string) = sys.argv[1:]\n\nprint '%s:%s Sending: %s\\n' % (host, port, string)\n\nconn = telnetlib.Telnet()\ntry:\n    conn.open(host, port)\n    conn.write('%s\\n' % (string))\n    print conn.read_all()\n    conn.close()\n\nexcept socket.error, (errno, errstr):\n    conn.close()\n    sys.exit("Error: %s!" % (errstr))\n


You're also a seasoned Perl programmer. The code that a new Perl programmer can easily create is best-described as "write only". Working Python code is nearly impossible to obfuscate without getting into some extremely esoteric constructs.
Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
New Not bad
I like the exception handling.
New What. He. Said.
Tht does it...I gotta go find a Python book somewhere...(Uhh, Scott...what animal does the Python book have on it? No, I don't believe the answer is that easy....)
jb4
shrub●bish (Am., from shrub + rubbish, after the derisive name for America's 43 president; 2003) n. 1. a form of nonsensical political doubletalk wherein the speaker attempts to defend the indefensible by lying, obfuscation, or otherwise misstating the facts; GIBBERISH. 2. any of a collection of utterances from America's putative 43rd president. cf. BULLSHIT

New WTF do YOU think it has on it...?
Sheesh. Yes, it IS that easy.

[link|http://www.oreilly.com/catalog/pythonian/|http://www.oreilly.c...atalog/pythonian/]

Now, the "Learning Python" book is a little different... it has prey on the cover: [link|http://www.oreilly.com/catalog/lpython2/|http://www.oreilly.com/catalog/lpython2/]
Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
Expand Edited by admin Sept. 16, 2005, 06:27:38 PM EDT
New I don't think you'll be surprised. (img)
[image|ftp://ftp.ora.com/pub/graphics/book_covers/hi-res/0596000855.jpg|0|Programming Python|360|250]

There are other books that have rats and stuff on the cover.

Cheers,
Scott.
New Hiss
The Programming Python book is large and rather difficult to read sequentially. Pretty good as a reference when you want to learn some aspects on a particular subject, but it is not really a book for learning how to program in Python. The Learning Python book is a good sequential read, but lacks a whole lot in terms of breadth. Best read Learning first, and then use Programming as a reference.

Or you could do like admin and just use the freely available web resources to learn the language.
New Wel, it's beautiful code
But won't it turn a course on TCP/IP into a course on Python networking libraries? The question is, what's the goal of the course? Is it to be able to do things on the net, or to understand how the net realy works?

------

179. I will not outsource core functions.
--
[link|http://omega.med.yale.edu/~pcy5/misc/overlord2.htm|.]

New Use the socket libraries then.
Readable code is more about structure and obviousness than libraries.
Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
New It's introduction to networking
but I have programmers in the class.

The idea was to give them just enough to shoot a few bytes across a LAN.

Give them a little Marconi Moment, as it were.

Another possibility is to show them some network hacking with nmap, Metasploit et al.
Tom Sinclair

The place looked as though it had been visited by Gengiz Cohen [footnote:
hence the term "wholesale destruction"].
-- (Terry Pratchett, Lords and Ladies)
New I'd go with socket basics.
It's not that difficult conceptually, and will make How It All Works much clearer than any other thing you can do. After all, once you get the connection made and can pass text back and forth, creating a service is just all about how it's processed at each end.

To my mind, I'd start with the socket basics, keep it quite basic, and point them at further info if they wish to follow up on their own. After seeing how sockets work, showing them the kinds of things you can do with nmap etc will be much much clearer to them. Start down near the root, and move the course further up the tree as it progresses.
--\n-------------------------------------------------------------------\n* Jack Troughton                            jake at consultron.ca *\n* [link|http://consultron.ca|http://consultron.ca]                   [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\n* Kingston Ontario Canada               [link|news://news.consultron.ca|news://news.consultron.ca] *\n-------------------------------------------------------------------
New Re: I'd go with socket basics.
Server:
#!/usr/bin/python\n\nimport sys, socket\n\nif (len(sys.argv) < 2):\n    sys.exit('Must supply port!\\n')\n\nport = sys.argv[1]\nhost = socket.gethostbyname(socket.gethostname())\n\nprint '%s:%s Listening:\\n' % (host, port)\n\nsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\ntry:\n    sock.bind((host, int(port)))\n    sock.listen(1)\n\n    conn, addr = sock.accept()\n    print 'Connection from: ' + str(addr)\n\n    str = conn.recv(1024)\n    print 'Echoing "%s"' % (str)\n    conn.send(str)\n\n    conn.close()\n\nexcept socket.error, (errno, errstr):\n    conn.close()\n    sys.exit("Error: %s!" % (errstr))


Client:
#!/usr/bin/python\n\nimport sys, socket\n\nif (len(sys.argv) < 4):\n    sys.exit('Must supply host, port, and text string to send!\\n')\n\n(host, port, string) = sys.argv[1:]\n\nprint '%s:%s Sending: %s\\n' % (host, port, string)\n\nsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\ntry:\n    sock.connect((host, int(port)))\n    sock.sendall('%s\\n' % (string))\n    recv = ''\n    while 1:\n        str = sock.recv(1024)\n        if not str: break\n        recv += str\n\nexcept socket.error, (errno, errstr):\n    sock.close()\n    sys.exit("Error: %s!" % (errstr))\n\nprint "Received:\\n" + recv
Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
New Didn't say it would take very long
:)

Of course, for the students that DON'T know programming, learning that will take quite a bit longer; OTOH, once they DO grok it, they will be much better positioned to correctly grok computers in general.

There are a LOT of users that have very, ummm, odd intellectual models of what's actually going on in there.

Cue Tom Waits as appropriate.
--\n-------------------------------------------------------------------\n* Jack Troughton                            jake at consultron.ca *\n* [link|http://consultron.ca|http://consultron.ca]                   [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\n* Kingston Ontario Canada               [link|news://news.consultron.ca|news://news.consultron.ca] *\n-------------------------------------------------------------------
New Programming Languages will influence the model
So one must be careful to choose a PL that has the proper level of abstraction for the lesson to be taught and the model to be molded. If you want them to understand it in terms of bits and bytes and machine instructions, C and it's ilk is the way to go. If you want them to understand it as a set of libraries that can be slowly dissected, then Python and similar languages can be the way to go.

Either way, both are really just models for the more abstract thought of network transport.
New Indeed
and I think a higher level view than C is the one for this. I might get a kick out of bit hacking from time to time, but I'm funny and strange.

The key concept is two apps, one with a listen socket and one connecting to it, exchanging data through a series of read and write operations to their end of the socket. Once they grok that, then things like the output (in OS/2) of
iptrace && ipformat
get a lot easier to figure out.
--\n-------------------------------------------------------------------\n* Jack Troughton                            jake at consultron.ca *\n* [link|http://consultron.ca|http://consultron.ca]                   [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\n* Kingston Ontario Canada               [link|news://news.consultron.ca|news://news.consultron.ca] *\n-------------------------------------------------------------------
New Definitely want high level languages

Keep this at the upper levels of the TCP/IP stack.


Tom Sinclair

Crowley was in Hell's bad books. Not that Hell has any other kind.
-- (Terry Pratchett & Neil Gaiman, Good Omens)
New Yep.

------

179. I will not outsource core functions.
--
[link|http://omega.med.yale.edu/~pcy5/misc/overlord2.htm|.]

     Simple network programming language? - (tjsinclair) - (51)
         Python. - (folkert)
         What Greg said. - (admin) - (3)
             That's exactly what I was thinking - (tjsinclair) - (2)
                 cautions, cause.. - (cforde) - (1)
                     Understood - (tjsinclair)
         REBOL gets you there the fastest - (ChrisR) - (4)
             Agreed. - (admin)
             I considered Java briefly - (tjsinclair) - (2)
                 Definietly not Java or anything with a compiler - (tuberculosis) - (1)
                     Granted - (tjsinclair)
         perl, creating sockets and listening is fairly easy -NT - (boxley) - (35)
             Thanks for the tip - (tjsinclair)
             But not as readable as Python. - (admin) - (33)
                 This isn't readable? - (broomberg) - (32)
                     Ben could reduce that to about 12 characters... ;-) -NT - (Another Scott) - (15)
                         Of course he could - (broomberg) - (14)
                             :-) Yup, but in Python one can't do that. - (Another Scott) - (11)
                                 Hmm - (broomberg) - (10)
                                     Write That Code! - (Another Scott)
                                     Sorry I'm late to the Party ... Ruby Version - (JimWeirich) - (8)
                                         Where's the error handling? - (admin) - (7)
                                             I wouldn't bet that it is missing - (ben_tilly) - (6)
                                                 OK, fine... but - (admin) - (5)
                                                     As I said... - (ben_tilly) - (4)
                                                         Near enough to Python, then. - (admin) - (3)
                                                             Re: Near enough to Python, then. - (JimWeirich) - (2)
                                                                 As I said... - (admin) - (1)
                                                                     Ahh, makes me long for the old days - (broomberg)
                             Oops. -NT - (inthane-chan)
                             ICJRLPD (new thread) - (inthane-chan)
                     As I said, "not AS readable as the Python" - (admin) - (15)
                         Not bad - (broomberg) - (4)
                             What. He. Said. - (jb4) - (3)
                                 WTF do YOU think it has on it...? - (admin)
                                 I don't think you'll be surprised. (img) - (Another Scott)
                                 Hiss - (ChrisR)
                         Wel, it's beautiful code - (Arkadiy) - (9)
                             Use the socket libraries then. - (admin)
                             It's introduction to networking - (tjsinclair) - (7)
                                 I'd go with socket basics. - (jake123) - (6)
                                     Re: I'd go with socket basics. - (admin) - (5)
                                         Didn't say it would take very long - (jake123) - (3)
                                             Programming Languages will influence the model - (ChrisR) - (2)
                                                 Indeed - (jake123) - (1)
                                                     Definitely want high level languages - (tjsinclair)
                                         Yep. -NT - (Arkadiy)
         I'd suggest Ruby... - (ben_tilly) - (2)
             I believe I stole that example from it - (broomberg)
             I'm thinking of quick and dirty HOWTO here - (tjsinclair)
         Consider Object REXX - (jake123) - (1)
             Thanks for the reminder - (tjsinclair)

A cheery thought that I shall leave you with...
252 ms