Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6953199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:26:53+00:00 2026-05-27T14:26:53+00:00

In the next examlpe I have Socket server handled in the thread. In the

  • 0

In the next examlpe I have Socket server handled in the thread. In the handle function we create Thread and pass request and client_address.
When we handle the request in run-method of the Thread it’s only possible to receive data, but when I try to send somthing an Exception is raised ([Errno 9] Bad file descriptor)
What’s wrong?

import SocketServer
import threading


class MyServerThread(threading.Thread):

    def __init__(self, channel, details):
        self.channel = channel
        self.details = details
        threading.Thread.__init__(self)

    def run(self):
        print 'Received connection:', self.details[0]
        self.channel.send('(Response)')
        print 'Received:', self.channel.recv(1024)
        self.channel.close()
        print 'Closed connection:', self.details[0]


class MyThreadedSocketServerHandler(SocketServer.BaseRequestHandler):

    def handle(self):
        thread1 = MyServerThread(self.request, self.client_address)
        thread1.start()

if __name__ == '__main__':
    server = SocketServer.TCPServer(('localhost', 8888), MyThreadedSocketServerHandler)
    server.serve_forever()


#---Client to test---
import socket

message = "(Request)"

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
    sock.connect(('localhost', 8888))
    sock.send(message)
    data = sock.recv(1024)
    print 'Sent:     %s' % message
    print 'Received: %s' % data
finally:
    sock.close()




#Working example without "join"
import socket
import threading

class ClientThread(threading.Thread):

    def __init__(self, channel, details):
        self.channel = channel
        self.details = details
        threading.Thread.__init__(self)

    def run(self):
        print 'Received connection:', self.details[0]
        self.channel.send('message')
        print self.channel.recv(1024)
        self.channel.close()
        print 'Closed connection:', self.details[0]

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('', 8888))
server.listen(5)

while True:
    channel, details = server.accept()
    ClientThread(channel, details).start()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:26:54+00:00Added an answer on May 27, 2026 at 2:26 pm

    I think you are looking for ThreadingMixIn:

    from SocketServer import TCPServer, ThreadingMixIn
    
    class MyServerThread(ThreadingMixIn, TCPServer): pass
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the next function: function setImagesWidth(id,width) { var images = document.getElementById(id).getElementsByTagName(img); for(var i
developers! I can't understand next situation For Example I have model class Pg::City <
I have next code: PhotoFactory factory = PhotoFactory.getFactory (PhotoResource.PICASA); PhotoSession session = factory.openSession (login,
I have a problem with a recursive SQL function. The original problem is that
I have a TCP socket connection where I get byte array of data which
If you have two threads invoking a static function at the same moment in
We have a .net service with simple commands (play, pause, next track, for example).
For example I have next python class class Myclass(): a = int b =
For example I have following jQuery code: var div1 = $(divs).next(); How do I
I have a server say 'XYZ' and many clients of different domain, say A,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.