I am trying to open multiple ports in a computer with PYTHON Sockets.
Here is my code..
import socket
import thread
from threading import *
BUFSIZ = 1024
clientsock = socket.socket()
def handler(clientsock,addr):
while 1:
data = clientsock.recv(BUFSIZ)
if not data:
break
def function():
for n in range(1,5):
var = 's'+str(n)
clientsock, addr = var.accept()
thread.start_new_thread(handler, (clientsock, addr))
return
for port in range(1,5):
var = 's'+str(port)
var = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
HOST = socket.gethostname()
var.bind((HOST,port))
var.listen(5)
while 1:
print 'Waiting for connection:'
function()
I am getting following error:
C:\Python27>python a2.py
Waiting for connection:
Traceback (most recent call last):
File "a2.py", line 25, in <module>
function()
File "a2.py", line 14, in function
clientsock, addr = var.accept()
AttributeError: 'str' object has no attribute 'accept'
C:\Python27>
What wrong in the code???
Any other way to do the same job.