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 6869463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:33:39+00:00 2026-05-27T03:33:39+00:00

I need to open a socket connection in a new thread. That connection needs

  • 0

I need to open a socket connection in a new thread. That connection needs to stay open. Then I need to be able to send data to the socket from the main scope.

I have a feeling what’s happening with my code is that the thread is completing & closing the socket right away and then there’s no properties to send data to.

How can I keep the thread & socket opened to receive data to send out from the main scope?

(If I take the threading out of this, it works fine.)

Below is the code and output I’m working with.

Here is the output from the shell:

$ python test.py 
Traceback (most recent call last):
  File "test.py", line 25, in <module>
    packet = mt.sendData('somedata')
  File "test.py", line 19, in sendData
    self.mySocket.send(myString)
AttributeError: 'NoneType' object has no attribute 'send'

And here is the code

note line 19 is: self.mySocket.send(myString)
line 25 is: packet = mt.sendData('somedata')

import threading
import socket

class MyTest(threading.Thread):
    def __init__(self, host, port):
        self.host = host
        self.port = port
        self.mySocket = None
        threading.Thread.__init__(self)

    def run(self):
        #open socket
        self.mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.mySocket.connect( ( self.host, self.port ) )
        self.mySocket.setblocking(1)

    def sendData(self, myString):
        # send data to socket
        self.mySocket.send(myString)
        packet = self.mySocket.recv(4096)


mt = MyTest('127.0.0.1', 50001)
mt.start()
packet = mt.sendData('somedata')
  • 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-27T03:33:39+00:00Added an answer on May 27, 2026 at 3:33 am

    You may find the example in this post useful – it demonstrates a thread that perform socket communication receiving commands from another thread via Queue objects. It’s a rather generic sample, well documented (both in comments and the linked blog post) and you can easily adapt it for any specific purpose.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to know how to open a TCP socket connection from Silverlight. How
I need to open a new window from code-behind on post-back if a specific
I need to send a txt file via socket connection when requested by client.
I need open source database that I can ship with my commercial application. It
I need to open foxpro free tables in vb.net using the oledb connection. But...
I need to open a link from within jQuery but i need to avoid
I need to open a TCP/IP client connection with my Netty Server business handler.
In socket programming in Linux I need to write data in socket but I
I'm writing web application where I need to push data from server to the
I need an applet to open a socket and communicate with a server listening

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.