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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:38:56+00:00 2026-06-14T22:38:56+00:00

Hello I am trying to port some C code to python. I haven’t used

  • 0

Hello I am trying to port some C code to python. I haven’t used python in a few months so feel a bit rusty.

Wondering how I can do this. I need to be able to use the send value of the sock object in createConnection function with out making the sock object global.

any ideas?

*cheers

Example code

def createConnection(host, port, tcpTimeout):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
    sock.connect((host, port))
    return sock

def useConnectionOne():
    sock = createConnection("<Some IP>", <Some Port>, 5)
    sock.send("Hello world")

def useConnectionTwo():
    sock = createConnection("<Some IP>", <Some Port>, 5)
    sock.send("Hello again world")
  • 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-06-14T22:38:57+00:00Added an answer on June 14, 2026 at 10:38 pm

    You either need to make the socket objects global so they can be re-used, or attach them to some other object / container to keep track of them. You could do something like this:

    Connections = {}
    Connections['host1'] = createConnection("<Some IP>", <Some Port>, 5)
    Connections['host2'] = createConnection("<Some Other IP>", <Some Port>, 5)
    

    Then you can send with:

    Connections['host1'].send("Hello World")
    

    Without some outer container though, your socket objects leave scope and are garbage collected when the function that creates them returns.

    If I misunderstand you somehow, and you want a single connection object and the ability write two methods that use it separately, create a class that contains the connection:

    class ConnManager(object):
      def __init__(self, host, port, timeout)
        self._conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
        self._conn.connect((host, port))
    
      def sendOne(self, data):
        self._conn.send(data)
    
      def sendTwo(self, data):
        self._conn.send(data)
    

    Hopefully that gives you an idea you can build from.

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

Sidebar

Related Questions

I am trying to port my python code to C, but before that i
Hello i'm trying to configure apache2 server for python, i used this command to
Hello I'm trying to read some data from the serial port and record it
Hello I'm trying to port a legacy application to python with sqlalchemy. The application's
Hello im trying to echo only the 50 letters, but something in my code
Hello I'm trying something .. I was reading Jquery traversing documentations some forums, this
I'm trying to test this 'hello world' of python (with tornado) on ubuntu: import
Ok, so I'm trying to get some UDP code working and I'm barely green
I'm just trying to compile the hello world example of boost.python WITHOUT using all
This is my second time using C++ and I'm trying to port some Java

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.