I am kind of new to python. 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() message = myCSocks[i].recv(1024)
I am getting the following error:
Traceback (most recent call last): File "./htmlserv_multi.py", line 22, in <module> 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. Thank you.
PS: My final objective is to create a very simple multithreaded TCP web server (using python)
CODE:
#! /usr/bin/env python from socket import * #does this work? myCSocks = [] serverSocket = socket(AF_INET, SOCK_STREAM) serverSocket.bind(('192.168.1.4',12000)) serverSocket.listen(5) while True: for i in range(0, len(myCSocks)+1): myCSocks[i], addr = serverSocket.accept() try: for i in range(0, len(myCSocks)): message = myCSocks[i].recv(1024) filename = message.split()[1] f = open(filename[1:]) outputdata = f.read() myCSocks[i].send('HTTP/1.1 200 OK\r\n\r\n') for p in range(0, len(outputdata)): myCSocks[i].send(outputdata[p]) myCSocks[i].close() except IOError: connectionSocket.send('HTTP/1.1 404 Bad Request\r\n\r\n') connectionSocket.send('<HTML><p>ERROR 404: BAD REQUEST!</p></HTML>') serverSocket.close() exit()