Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple TCP Server
#1
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.

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)
Reply
#2
You should just try Python's http.server
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
(May-11-2017, 02:58 AM)shift838 Wrote:     def run(self):
        while 1:
            print('Client sent:', self.sock.recv(1024).decode())
            self.sock.send(msg)
self.sock.close()?

If you're doing anything more than just messing around, you should probably use something designed for being fast at a low level, like Twisted (http://twistedmatrix.com/trac/)
Reply
#4
(May-11-2017, 04:42 PM)nilamo Wrote:
(May-11-2017, 02:58 AM)shift838 Wrote:     def run(self):
        while 1:
            print('Client sent:', self.sock.recv(1024).decode())
            self.sock.send(msg)
self.sock.close()?

If you're doing anything more than just messing around, you should probably use something designed for being fast at a low level, like Twisted (http://twistedmatrix.com/trac/)

I tried that already.  And I just get the typical 

File "mysocksvr2.py", line 22, in <module>
    self.sock.close
NameError: name 'self' is not defined

(May-11-2017, 02:14 PM)sparkz_alot Wrote: You should just try Python's http.server

My clients are not connecting via the http protocol but a telnet protocol from older 8 bit computers.
Reply
#5
If self isn't defined, then how is self.sock.send(msg) working?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Paramiko Server -- Exception (server): Error reading SSH protocol banner ujlain 3 4,536 Jul-24-2023, 06:52 AM
Last Post: Gribouillis
  Simple TCP Client and TCP Server Problem Vapulabis 5 4,397 Jul-12-2020, 05:09 PM
Last Post: ndc85430
  Selector file descriptor issue in simple multiclient server program dohpam1ne 0 2,926 May-28-2020, 02:39 PM
Last Post: dohpam1ne
  simple udp server/client cardmaker 2 3,577 Nov-26-2019, 12:36 AM
Last Post: micseydel
  Simple send and recive string Server/Client Epilepsy 1 2,738 May-01-2018, 08:17 PM
Last Post: ThiefOfTime

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020