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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:51:19+00:00 2026-06-10T01:51:19+00:00

I am pasting whole code, since I think it is as short as it

  • 0

I am pasting whole code, since I think it is as short as it can get;

import socket

def recv_all(sock):
    parts = []
    while True:
        msg = ""
        msg = sock.recv(4096)
        print "recieved", len(msg), "bytes."
        if not msg:
            break
        parts.append(msg)
    return "".join(parts)

http_request = """GET / HTTP/1.1
Host: {}
User-Agent: Opera/9.80 (Windows NT 6.1; WOW64; U; tr) Presto/2.10.229 Version/11.64
Accept: text/html, application/xml;q=0.9, application/xhtml xml, image/png, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1
Accept-Language: en
Accept-Charset: iso-8859-1, utf-8, utf-16, *;q=0.1
Accept-Encoding: deflate, gzip, x-gzip, identity, *;q=0
Connection: Keep-Alive, TE
TE: deflate, gzip, chunked, identity, trailers


"""
site = "www.google.com.tr"
print "creating connection"
conn = socket.create_connection((site,80))
print "sending data"
conn.sendall(http_request.format(site))
print "recieving response"
response = recv_all(conn)
print "closing connection"
conn.close()

print response

When I run this, I get this output;

creating connection
sending data
recieving response
recieved 3743 bytes.
recieved 4096 bytes.
recieved 4096 bytes.
recieved 4096 bytes.
recieved 3472 bytes.
recieved 1648 bytes.

What I expected it to do is to read 0 bytes after it reads last chunk (1648 in this case). But it keeps waiting instead.

I am on windows 7 with Python 2.7.

What am I doing wrong here? What should I do so that it works as intended?

  • 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-10T01:51:21+00:00Added an answer on June 10, 2026 at 1:51 am

    You’re sending:

    Connection: Keep-Alive, TE
    

    … and the server’s doing exactly as you asked – keeping the connection alive at the end. So your read call blocks, waiting for more data – while the server is waiting for another request on the same connection.

    Either turn off keep-alive, or look for a Content-Length header, and stop reading when you’ve read that much data.

    (Alternatively, use an HTTP client library instead of trying to do it yourself 🙂

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

Sidebar

Related Questions

I have a problem while pasting my contents (or text) generated by Java code
There is a problem executing this block of code while passing the parameter. Parameter
i am using mvc 3 code first. facing problem while passing data form SecurityAttribute
How can I instantiate an anonymous object while passing the propertynames and values as
While walking through a sample code application for the iPhone, I found a pair
For class I'm creating a shell in C++. It's been a while since I've
i've been playing with MVC for a while now, but since the project i'm
hie i have a html form and while posting a request to a url
I have a Microsoft dynamics Ax client instance. While posting packing slip on Sales
Passing User defined argument to RPM is possible while installing?. for example: ~>rpm -i

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.