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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:24:46+00:00 2026-05-23T08:24:46+00:00

I am doing some socket programming in python in which I develop a client

  • 0

I am doing some socket programming in python in which I develop a client TCP/IP socket to communicate with a slow embedded device. So sometimes, when the response is supposed to be only one package, it is broken in to two packages. My current solution to that is to sleep() the python program, waiting to make sure that all the data I need already arrived.

comSocket.send('\r')

sleep(1)

comSocket.send('\r')

comSocket.recv(128)

comSocket.send('\r\r')

comSocket.recv(256)



#sending I commands for data

comSocket.send('1I\r\r3I\r\r4I\r\r13I\r\r5I\r\r8I\r\r7I\r\r9I\r\r')

sleep(2)

#receiving data

rawData = comSocket.recv(512)

sleep(1.5)

I am wondering whether there is a better way to handle this situation?

  • 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-23T08:24:47+00:00Added an answer on May 23, 2026 at 8:24 am

    If it is a single device, you should use a solution that handles the low-level socket interactions for you… python has several like pexpect, exscript, or paramiko (ssh only)… if it is multiple devices and you need asynchronous communication, use @zeekay’s answer (although async programming, particularly with twisted, is unpleasant if you’re not already familiar with it).

    I answered a question with the way you telnet and send a list of commands to a single device here…

    Catching a dying process in pexpect

    The answer above makes the code efficient, but is more challenging for a beginner to understand… the following code is simpler, it makes a telnet connection on TCP/23, waits for *, sends a command, and puts the response to the command in mydata1…

    import pexpect as px
    import sys
    
    def send_this(child, retcode, COMMAND):
        if retcode == 2:
            child.sendline(COMMAND)
        else:
            raise RuntimeError, "Could not complete login, due to socket error"
    
    def expect_this(child, EXPR, timeout = 10):
        return child.expect([px.TIMEOUT, px.EOF, EXPR], timeout = timeout)
    
    HOST = '192.168.49.49'
    CMD1 = '1I'
    PROMPT = '\*'     #Note: you might not need the backslash
    TIMEOUT = 10
    
    child = px.spawn('telnet %s' % HOST)
    retcode = expect_this(child, PROMPT)
    send_this(child, retcode, CMD1)
    retcode = expect_this(child, PROMPT)
    mydata1 = child.before    # mydata has the output from CMD1
    

    Regardless of the solution, the most important thing is tweaking your timeouts so you don’t have to concern yourself with the long cell-network delays (which are sometimes over 5 seconds in my experience).

    The other nasty dynamic in cellular communication is that many consumer devices routinely change their IP address when they are moving (due to their reliance on dhcp)… there isn’t much TCP can do if this happens… if you don’t have a static address the connection will drop when the device pulls a new ip address.

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

Sidebar

Related Questions

I'm doing some fairly simple cross-platform TCP socket programming. I have unfortunately found out
I've been doing some socket programming to transmit information across the wire. I've run
I am currently doing some socket programming using C/C++. To be able to use
I am doing some socket programming in C, and trying to wrestle with byte
Linux GCC 4.4.2 I am doing some socket programming. However, I keep getting this
I'm doing some lovely socket programming in objective-C right now and part of my
I'm including the netinet headers and doing some raw socket programming on the iPhone,
Doing some client-side JavaScript development, which requires me to call a client-side API which
Currently I'm doing some unit tests which are executed from bash. Unit tests are
I'm doing some networking programming for Windows right now envolving the HTTP protocol. Is

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.