How do I make a server listen on multiple ports

2024/11/16 20:48:27

I would like to listen on 100 different TCP port with the same server. Here's what I'm currently doing:-

import socket
import selectdef main():server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)for i in range(1000,1100):server_socket.bind(('127.0.0.1', i))server_socket.listen(1)read_list = [server_socket]while True:readable, writable, exceptional = select.select(read_list, [], read_list)for s in readable:if s is server_socket:#print "client connected"client_socket, address = server_socket.accept()read_list.append(client_socket)else:# One of the tcp clientsdata = s.recv(1024)if not result:s.close()read_list.remove(s)#print "client disconnected"if __name__ == "__main__":main()

It returns an error saying Permission Denied. Is it because some ports(1000-1100) are reserved and are not allocated to it or because of some other reason?

I tried with (8000-8100) and it says Invalid Arguments

EDITED

import socket
import selectdef create_socket(TCP_PORT):server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)server_socket.bind(('127.0.0.1', TCP_PORT))server_socket.listen(1)return server_socketdef main():read_list = []for TCP_PORT in range(8000,8100):read_list.append(create_socket(TCP_PORT))while True:readable, writable, exceptional = select.select(read_list, [], read_list)for s in readable:if s is server_socket:#print "client connected"client_socket, address = server_socket.accept()read_list.append(client_socket)else:# One of the tcp clientsdata = s.recv(1024)if not result:s.close()read_list.remove(s)#print "client disconnected"if __name__ == "__main__":main()
Answer

There are 2 problems.

  1. Ports below 1024 are reserved. (You'll need special privileges, e.g. root privileges to bind to such a port).

  2. A socket can only listen at one port. So to listen to several port, you need to create one socket per port.

https://en.xdnf.cn/q/71630.html

Related Q&A

Django REST Framework: return 404 (not 400) on POST if related field does not exist?

Im developing a REST API which takes POST requests from some really brain-dead software which cant PATCH or anything else. The POSTs are to update Model objects which already exist in the database.Spec…

How can i login in instagram with python requests?

Hello i am trying to login instagram with python requests library but when i try, instagram turns me "bad requests". İs anyone know how can i solve this problem?i searched to find a solve f…

How to abstract away command code in custom django commands

Im writing custom django commands under my apps management/commands directory. At the moment I have 6 different files in that directory. Each file has a different command that solves a unique need. How…

Python one-liner (converting perl to pyp)

I was wondering if its possible to make a one-liner with pyp that has the same functionality as this.perl -l -a -F, -p -eif ($. > 1) { $F[6] %= 12; $F[7] %= 12;$_ = join(q{,}, @F[6,7]) }This takes i…

Getting symbols with Lark parsing

Im trying to parse a little pseudo-code Im writing and having some trouble getting values for symbols. It parses successfully, but it wont return a value the same as it would with "regular" …

Why cant I connect to my localhost django dev server?

Im creating a django app and in the process of setting up my local test environment. I can successfully get manage.py runserver working but pointing my browser to any variation of http://127.0.0.1:8000…

How to use L2 pooling in Tensorflow?

I am trying to implement one CNN architecture that uses L2 pooling. The reference paper particularly argues that L2 pooling was better than max pooling, so I would like to try L2 pooling after the acti…

Abstract base class is not enforcing function implementation

from abc import abstractmethod, ABCMetaclass AbstractBase(object):__metaclass__ = ABCMeta@abstractmethoddef must_implement_this_method(self):raise NotImplementedError()class ConcreteClass(AbstractBase)…

Create dataframe from dictionary of list with variable length

I have a dictionary of list which is like - from collections import defaultdict defaultdict(list,{row1: [Affinity],row2: [Ahmc,Garfield,Medical Center],row3: [Alamance,Macbeth],row4: [],row5: [Mayday]}…

How to standardize ONE column in Spark using StandardScaler?

I am trying to standardize (mean = 0, std = 1) one column (age) in my data frame. Below is my code in Spark (Python):from pyspark.ml.feature import StandardScaler from pyspark.ml.feature import VectorA…