Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

socket always listen in thread python

import socket
from threading import Thread
from cmd import Cmd

# basic threading tutorial: https://www.tutorialspoint.com/python3/python_multithreading.htm

class ThreadedServer(Thread):

  def __init__(self):
    Thread.__init__(self)  # change here
    self.host = "127.0.0.1"
    self.port = int(8080)
    self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    self.sock.bind((self.host, self.port))

  def run(self):    # change here
    self.sock.listen(5)
    print("[Info]: Listening for connections on {0}, port {1}".format(self.host,self.port))
    while True:
        print("Hello?") # Just debug for now
        client, address = self.sock.accept()
        client.settimeout(60)
        Thread(target = self.listenToClient, args = (client,address)).start()   # change here

  def listenToClient(self, client, address):
    size = 1024
    while True:
        try:
            data = client.recv(size)
            if data:
                # Set the response to echo back the recieved data
                response = data
                client.send(response)
            else:
                raise error('Client disconnected')
        except:
            client.close()
            return False


class CommandInput(Cmd):
  # Able to accept user input here but is irrelevant right now
  pass

if __name__ == "__main__":
  print("[Info]: Loading complete.")

  server = ThreadedServer()  # change here
  server.start()  # change here

  print("[Info]: Server ready!")

  prompt = CommandInput()
  prompt.prompt = '> '
  prompt.cmdloop("[Info]: Type 'help' for a list of commands and their descriptions/use")
Comment

PREVIOUS NEXT
Code Example
Python :: pandas count 
Python :: wikipedia python module 
Python :: Invalid password format or unknown hashing algorithm. 
Python :: send mail through python 
Python :: sorting values in dictionary in python 
Python :: Set symmetric Using Python Set symmetric_difference() Method 
Python :: python series unique 
Python :: remove first element from list 
Python :: add reaction discord.py 
Python :: pretty printing using rich library in python 
Python :: Scrapping tables in an HTML file with BeautifulSoup 
Python :: beautifulsoup usage 
Python :: python convert object to json 
Python :: pyqt5 qtextedit change color of a specific line 
Python :: ram clear in python 
Python :: tf MaxPooling2D 
Python :: installing pip in pytho 
Python :: change value in excel in python 
Python :: python save variable to file pickle 
Python :: count repeated strings map python 
Python :: save to xlsx in python 
Python :: count how much a number is in an array python 
Python :: print list in one line 
Python :: animations on canvas tkinter 
Python :: pip offline package install 
Python :: epoch to gmt python 
Python :: dataframe pandas empty 
Python :: how to change the disabled color in tkinter 
Python :: pandas dataframe display cell size 
Python :: how to set gpu python 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =