top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

array/list of sockets in python

+5 votes
444 views

I am currently trying to make and use a list/array of sockets in a program. So I have declared an array as follows:

myCSocks = ['CSock1', 'CSock2', 'CSock3', 'CSock4', 'CSock5']

and I am trying to use my array elements as follows:

myCSocks[i], addr = serverSocket.accept()
 and 
message = myCSocks[i].recv(1024)

I am getting the following error:

Traceback (most recent call last):
 File "./htmlserv_multi.py", line 22, in 
 message = myCSocks[i].recv(1024)
AttributeError: 'str' object has no attribute 'recv'

This kind of makes sense to me, it is saying that my array elements are of type String and are not sockets. So I understand what my problem is but I do not know how to remedy it. I have googled "list of sockets python" but did not find anything. Any help will be greatly appreciated.

posted Oct 26, 2013 by Bob Wise

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
myCSocks = ['CSock1', 'CSock2', 'CSock3', 'CSock4', 'CSock5']
This is a list of Python strings, not an array, not sockets.

1 Answer

+1 vote

You actually have added strings to your list. this 'is_a_string. You need to add the objcts to the list. E.g.:

myCSocks = [CSock1, CSock2, CSock3, CSock4, CSock5]

Hope this helps.

answer Oct 26, 2013 by Kumar Mitrasen
Similar Questions
+2 votes

I have a numpy array consisting of 1s and zeros for representing binary numbers:

e.g.

 >>> binary
 array([ 1., 0., 1., 0.])

I wish the array to be in the form 1010, so it can be manipulated. I do not want to use built in binary converters as I am trying to build my own.

0 votes

Can python sockets be used to capture IP traffic when the traffic is originating from a non-python source?

Using a Lantronix UDS-1100 serial to IP converter. The goal is to write a small proof of concept piece in Python to capture serial data output by this device over IP.

I've done a couple test projects using sockets in python, but they were all done between python processes (python > python): listen() on one end, and connect(), sendall() etc on the other.

I think I can use sockets for this project, but before I invest a bunch of time into it, wanted to make sure it is a viable solution.

Can python sockets be used to capture IP traffic when the traffic is originating from a non-python source? I have full control over the IP and port that the device sends the serial data to, but there will be no python connect() initiated by the client. I can pre-pend then serial data with some connect() string if needed.

If sockets won't work, please recommend another solution...guessing it will be REST or similar.

...