Live data from Hacker News

Python socket programming tutorial

binarytides.com

11–20 of 22 posts

Re: Python socket programming tutorial

#11
post #3

Shouldn't the first step be install http://www.zeromq.org/ ? That is some seriously amazing stuff right there. Makes network programming (something I've avoided for years) almost trivial.

The point is (or should be) to learn how socket programming works so you know what's going on under all that abstraction and why. That's also why we catch all those exceptions: so we understand why something failed, and how to deal with it in the future. It's also really fun to write complex networking apps from scratch =)

>It's also really fun to write complex networking apps from scratch

I agree and disagree. It definitely is fun to write awesome complex code, in general, but I'm currently in the middle of developing a network application and the most frustrating part was the socket handling. Everything riding on top of these connections was fun, but actually working out the TCP logic was un-fun.

That said, diff'rent strokes for diff'rent folks, I'm sure you're not the only one to love TCP coding, and I'm glad you guys exist to write tutorials for people like me :)

Re: Python socket programming tutorial

#12
If you want to understand sockets, there's no excuse for not reading Beej's Guide to Network Programming [1]. It's C, not Python, but it covers all of the dirty details and explains exactly what's going on when you create a socket. For instance, this article never mentions that sockets are file descriptors — in Beej's article, it's right after the introduction (Section 2 [2]).

How does this article describe a socket?

    > sockets are the fundamental "things" behind any kind of network communications done by your computer"
Not very helpful.

[1] http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html

[2] http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html...

Re: Python socket programming tutorial

#14

Earlier quoted context omitted.

The point is (or should be) to learn how socket programming works so you know what's going on under all that abstraction and why. That's also why we catch all those exceptions: so we understand why something failed, and how to deal with it in the future. It's also really fun to write complex networking apps from scratch =)

>It's also really fun to write complex networking apps from scratch I agree and disagree. It definitely is fun to write awesome complex code, in general, but I'm currently in the middle of developing a network application and the most frustrating part was the socket handling. Everything riding on top of these connections was fun, but actually working out the TCP logic was un-fun. That said, diff'rent strokes for diff…

I write modules to decode network protocols for fun. So yeah, maybe i'm not quite right in the head.

On the other hand, it taught me about perverted networking standards like 802.1Q which is more common than you'd expect, and helps when you're trying to figure out why tcpdump isn't returning results you expected.

Re: Python socket programming tutorial

#15
post #3

Shouldn't the first step be install http://www.zeromq.org/ ? That is some seriously amazing stuff right there. Makes network programming (something I've avoided for years) almost trivial.

The point is (or should be) to learn how socket programming works so you know what's going on under all that abstraction and why. That's also why we catch all those exceptions: so we understand why something failed, and how to deal with it in the future. It's also really fun to write complex networking apps from scratch =)

If that was the point we should all break out our chemistry sets to refine elements so we can create our logic chips and build up from there.

At some point you have to admit you've climbed down enough turtles and it's time to get some shit done. I'm claiming ZeroMQ is far enough down. It's not that much abstraction. It's mostly avoiding you the annoyances and giving you framework of best practices.

Re: Python socket programming tutorial

#16
post #3

Shouldn't the first step be install http://www.zeromq.org/ ? That is some seriously amazing stuff right there. Makes network programming (something I've avoided for years) almost trivial.

You can't always choose the protocol you want. So no, the first step is not necessarily to use ZeroMQ, or HTTP, or Thrift, or whatever protocol you like best. Sometimes raw sockets are necessary.

Re: Python socket programming tutorial

#17
post #3

Shouldn't the first step be install http://www.zeromq.org/ ? That is some seriously amazing stuff right there. Makes network programming (something I've avoided for years) almost trivial.

More appropriately, the first step should be to install Twisted.

Or, if you'd rather stay out of Callback Hell, you could go with the alternate approach of not using Twisted. Personally, I'm a fan of Eventlet:

http://eventlet.net/doc/

I also hear good things about Gevent, which is similar, though not nearly as well-documented. Or just use vanilla Python; sometimes it's all you need.

Re: Python socket programming tutorial

#18

Earlier quoted context omitted.

The point is (or should be) to learn how socket programming works so you know what's going on under all that abstraction and why. That's also why we catch all those exceptions: so we understand why something failed, and how to deal with it in the future. It's also really fun to write complex networking apps from scratch =)

If that was the point we should all break out our chemistry sets to refine elements so we can create our logic chips and build up from there. At some point you have to admit you've climbed down enough turtles and it's time to get some shit done. I'm claiming ZeroMQ is far enough down. It's not that much abstraction. It's mostly avoiding you the annoyances and giving you framework of best practices.

Each hacker choses how far down the rabbit hole they go, don't disparage others for going deeper.

Re: Python socket programming tutorial

#19

If you want to understand sockets, there's no excuse for not reading Beej's Guide to Network Programming [1]. It's C, not Python, but it covers all of the dirty details and explains exactly what's going on when you create a socket. For instance, this article never mentions that sockets are file descriptors — in Beej's article, it's right after the introduction (Section 2 [2]). How does this article describe a socket?…

I came here to say just this. Anyone who wants to do real socket programming should learn some of the low level reasons for how sockets work behind the scenes. Plus, it's a great read! (I especially like his story about IPv6)

Re: Python socket programming tutorial

#20
post #2

Is there a reason to catch all those exceptions? If you're just sys.exit()ing anyways, why sacrifice all the traceback info?

You should always assume, that a best effort network fails. Therefore, you should always handle the exceptions ( even in a trivial example).
Post reply on HN