0

Continuing from the question mentioned in the below link

list/array of sockets in python

Is it possible to create an array of sockets in python like

list=[socket0, socket1, socket2, socket3 ]

for i in range(0,3):
    list[i]=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    list[i].connect((host,port[0]))

I tried this but I'm getting the same error as I have posted in the link that no attribute recv.

Is it possible to create an array like this in python. I know it's possible in C.

Community
  • 1
  • 1
soma
  • 93
  • 3
  • 9
  • don't you mean `list[i]=socket.socket...`? – Joel Cornett Dec 07 '15 at 06:39
  • 1
    You can create a list containing any object you like, hell even classes (which are objects too in python). Start with an empty list and add your instantiated socket objects. Judging by your questions however, I get the feeling that you should start by understanding simple programming concepts first. – enpenax Dec 07 '15 at 06:44

2 Answers2

2

You should not pre-populate your list, but create it on the fly.

There are two way how you can do that:

  1. The "better" way:

    sockets = [socket.socket(socket.AF_INET, socket.SOCK_STREAM) for _ in range(3)]
    for sock in sockets:
         sock.connect((host, port[0]))
    
  2. The inferior way:

    sockets = []
    for i in range(3):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.connect((host, port[0]))
        sockets.append(sock)
    

Despite the extra iteration, the first one is better because it uses one of Pythons "best" festures for constructing a list and is shorter and more readable. The extra iteration's timing requirements is low to non-existent.

However, there is nothing which is really against the second one, so if you prefer it although it is longer, use that.

glglgl
  • 89,107
  • 13
  • 149
  • 217
  • Can you give a reasoning for why the list comprehension way is better? Now you are iterating twice. (Dont get me wrong I like the idea and what it does for readability, but OP does not seem to be very experienced, so maybe explain what makes it superior) – enpenax Dec 07 '15 at 06:50
  • 1
    @enpenax both options have `connect` call. It is a network IO operation and I suspect it is going to last enough so we can say that the other code took a negligible amount of time to execute no matter how many loops are there. So it's all about readability. – u354356007 Dec 07 '15 at 11:43
  • @enpenax The mere iterating doesn't cost so much that it would outweigh the readability. Note that it is about 4 list entries... – glglgl Dec 07 '15 at 13:36
  • As I said, don't get me wrong. I fully agree, but it is easy to write "this solution is superior" without giving the reasoning in the post :) Now I can upvote this :P but maybe it will never be marked as accepted :/ – enpenax Dec 08 '15 at 01:46
0

The fastest way:

import socket
sockets = list(map(lambda x: x.connect(('127.0.0.1', 80)), [socket.socket(socket.AF_INET, socket.SOCK_STREAM) for _ in range(3)]))

List comprehension works faster than creation of loop with continuous append or list[i], map function does too.

More beautiful way(slightly less efficient):

import socket

not_connected_sockets = [socket.socket(socket.AF_INET, socket.SOCK_STREAM) for _ in
    range(3)]

sockets = list(map(lambda x: x.connect(('127.0.0.1',
    80)), not_connected_sockets))
Andrey
  • 146
  • 1
  • 2
  • 8