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

  • Home
  • SEARCH
  • 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 7689623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:12:26+00:00 2026-05-31T20:12:26+00:00

Following is the code which listens on a port for HTTP requests and sends

  • 0

Following is the code which listens on a port for HTTP requests and sends the request packet to the server running on port 80, gets the response and sends the data back to the client. Now, everything is executing fine but the following line of code :

data = req_soc.recv(1024)

is taking too much time to execute and I have observed that, it takes long time to execute when it is going to/has received the last packet. I have also tried the same code using select.select() but the results are the same. Since I want to handle the data (raw) that is coming from the client and the actual HTTP server, I have no other choice than using sockets.

import socket
import thread

def handle_client(client):
    data = client.recv(512)
    request = ''
    request += data
    print data

print '-'*20
spl = data.split("\r\n")

print spl[0]
print spl[1]

if len(request):
    req_soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    req_soc.connect(('localhost', 80))
    req_soc.send(request)

    response = ''
    data = req_soc.recv(1024)
    while data:
        response += data
        print 1
        data = req_soc.recv(1024)
    req_soc.close()
    print response
    if len(response):
        client.send(response)
client.close()

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

print("Server is running...\n")

MSGLEN = 1024

while 1:

    client, address = server.accept()
    thread.start_new_thread(handle_client, (client, ))
  • 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-31T20:12:27+00:00Added an answer on May 31, 2026 at 8:12 pm

    Clients can do multiple commands (eg: GET) within one connection. You cannot wait for the client to send all the commands because based on what you return it could request more (eg: images of a web page). You have to parse the parts (commands) of request, find the boundary, forward that request to the server and write back the answer to the client. All this in a way that doesn’t block on reading the client.

    I’m not sure what’s the best way to do this in python, but if you spend 5 minutes of googling you’ll find a perfect HTTP proxy library.

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

Sidebar

Related Questions

I have the following piece of code to listen to a port which is
I have the following code (I'm working from code at http://www.linuxhowtos.org/C_C++/socket.htm ) which I'm
I have written a java socket server program which listens to a port continuously.
I have the following code which works just fine when the method is POST,
I have the following code which works fine. However, I only want to return
Consider the following code which is typcial of many ChannelFactory examples: WSHttpBinding myBinding =
I have the following code which should put programs startable in Bash. if [
i've got the following code which runs a bat file. the bat file then
I have the following code which reads in the follow file, append a \r\n
I have the following code which re-uses a CookieContainer which logs in on 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.