May-11-2017, 02:58 AM
I am very new to Python scripting.
I want to create a simple TCP server that will listen on a specified port and as soon as a connection is made it will display a message to the user connected over the socket then close the connection and wait for the next connection.
I found the below code and I have modified it a bit to display the message I want it to, but I want it to close the connect after my message is displayed
How can I close the below connection? I would even like to be able to close the connection after instead after the first message is displayed after the connection.
I want to create a simple TCP server that will listen on a specified port and as soon as a connection is made it will display a message to the user connected over the socket then close the connection and wait for the next connection.
I found the below code and I have modified it a bit to display the message I want it to, but I want it to close the connect after my message is displayed
How can I close the below connection? I would even like to be able to close the connection after instead after the first message is displayed after the connection.
import socket
from threading import *
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "192.168.1.104"
port = 8888
print (host)
print (port)
serversocket.bind((host, port))
msg = b'Connected to FuSiON BBS!\n\r\n\rCurrently Down\n\r\n\rPlease call back later...\n\r\n\r'
class client(Thread):
def __init__(self, socket, address):
Thread.__init__(self)
self.sock = socket
self.addr = address
self.start()
def run(self):
while 1:
print('Client sent:', self.sock.recv(1024).decode())
self.sock.send(msg)
####WANT TO CLOSE HERE###
serversocket.listen(5)
print ('server started and listening')
while 1:
clientsocket, address = serversocket.accept()
clientsocket.send(b'[--- Press Enter ---]\n\r\n\r')
client(clientsocket, address)
