[Tutor] Socket connection

Øyvind python at kapitalisten.no
Sun Dec 4 17:19:55 CET 2005


Hello.

I need to get some whois-info from whois.ripe.net. I have been able to
connect and get the info that I need, but I would like to know what is the
most 'polite' way of doing so. I need to do quite a few whois lookups
(from my server logs) and don't want to risk creating more hassle for
their server than necessary. So, what I would like to know, is what is the
best way of doing lots of requests?

This works like a dream:
>>> from socket import *
>>> import socket
>>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> s.connect(('whois.ripe.net', 43))
>>> s.send("%s \n\n" % 'vg.no')
14
>>> data = s.recv(8196)

But if I thereafter do this:
>>> s.send("%s \n\n" % 'dagbladet.no')
11
>>> data = s.recv(8196)
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
error: (10053, 'Software caused connection abort')

It doesn't go quite as well. I saw on
http://www.amk.ca/python/howto/sockets/ that the socket is supposed to get
destroyed after one use.

But, if I try to connect again:
>>> s.connect(('whois.ripe.net', 43))
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "<string>", line 1, in connect
error: (10056, 'Socket is already connected')

It is already connected.

So it seems like I have to start again at the beginning:
>>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> s.connect(('whois.ripe.net', 43))
......

But, I have 300 requests. Should I do this 300 times? And thereafter end
with:

>>> s.shutdown(1)
>>> s.close()

Or should I do this each time?

Or is there some other way to keep it open so that I can ask 300 times?

Thanks in advance


-- 
This email has been scanned for viruses & spam by Decna as - www.decna.no
Denne e-posten er sjekket for virus & spam av Decna as - www.decna.no



More information about the Tutor mailing list