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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:09:10+00:00 2026-05-31T14:09:10+00:00

I’m trying to implement Window socket using Python. Mostly, everything has been so far

  • 0

I’m trying to implement Window socket using Python.

Mostly, everything has been so far solved using ctypes.windll.ws2_32 and pywin32 lib.

However, I haven’t been able to find out how to translate the following C++ codes into Python and I wonder if anyone is kind enough to help:

LRESULT WINAPI AsyncSocketProc(
                           __in  HWND hwnd,
                           __in  UINT uMsg,
                           __in  WPARAM wParam,
                           __in  LPARAM lParam
                          )
switch(uMsg) {
    case WM_CREATE: 
        //...
    case WM_SOCKET: {# this is basically an int constant
        switch(WSAGETSELECTEVENT(lParam)){
              case FD_ACCEPT:
                  //accepting new conn
              case FD_READ:
                  //receiving data 
        }
    }
}

In the above code, I couldn’t find Python’s equivalent for WSAGETSELECTEVENT.

For the FD_ACCEPT, FD_READ, I could find them inside win32file package (of pywin32 lib)

Lastly, the reason why I’m trying to implement this Window socket programming is that the C++ version of the window socket server (above) is non-blocking for an application of mine but Python’s built-in select.select is blocking. So I’m trying to see if I can port the C++ version to Python and see if it works.

EDITED:
I would like to clarify that the socket server works as a ‘plug in’ to an existing C++ program, which doesn’t support threading.

The socket server needs to wait (indefinitely) for clients to connect so it needs to continuously listen.
So using a normal Python socket or select.select would entail a while loop (or otherwise how can it acts as a server continuously listening for events? Please correct me I’m wrong), which would block the main program.

Somehow, using the Window Socket server callback above, the main program is not blocked. And this is the main reason while I’m trying to port it to Python.

The socket server is preferably in Python because many related libs the server needs are written in Python.

Thanks a lot.

  • 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-31T14:09:11+00:00Added an answer on May 31, 2026 at 2:09 pm

    Have a look at the socket module instead. It already contains all the code you need to work with sockets without using the win32 API.

    [EDIT] You can write multi threaded code that can handle several connections. Just accept the connection and then start a new thread, give it the connection and let it read the data in a while 1: data = conn.recv(1024) ... kind of loop.

    That said, Python also has a module for just that: SocketServer

    [EDIT2] You say

    the socket server works as a ‘plug in’ to an existing program, which doesn’t support threading.

    It’s a bit hard to help with so little information but think about it this way:

    You can run the socket server loop in a new thread. This code is isolated from the rest of your app, so it doesn’t matter whether the other code uses/supports threads. This solves your “endless loop” problem.

    Now this socket server loop will get connections from clients. My guess is that the clients will call methods from the rest of the app and here, things get hairy.

    You need a way to synchronize these calls. In other places (like all UI frameworks), there is a single thread which runs any UI calls (drawing something, creating the UI, responding to user input).

    But if I understand you correctly, then you can in fact modify the “main loop” of the existing app and let it do more things (like listening to new connections). If you can do this, then there is a way out:

    Create a new thread for the socket server as described above. When the server gets a connection, spawn a new thread that talks to the client. When the client sends commands, create “work objects” (see command pattern) and put them into a queue.

    In the main loop, you can look into the queue. If something is in there, pop the work objects and call it’s run() method.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the

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.