0

I have a simple webserver and 1 simple index.html webpage with only 1 image element in it to send to the web client.

I have a Chrome web browser run on an Android Phone to get that webpage from that web server.

I have a problem.

Web server's code can get the first request message sent from the web client but not the second one.

When the web client send the first request, Web server's code can print the request. This mean recv() method is good. I mean recv() method is able to get that web client first request and also Web server can send the first response back to that web client with index.html webpage bytes but not along the image bytes, because image bytes will be sent by that webserver after that web client sent the second request as I will explain below.

But when the web client sent the second request to the web server to get that image of that webpage as what the web client should do in order to get complete webpage, the web server's code blocked or stuck on recv() insruction forever without getting that second request from that web cliet. In other side, Wireshark captured that second request from that web client. It means that web client indeed sent that second request to the Web server computer and it arrived at the web server computer. But why recv() can't get that second request? Why recv() only can get the first request from that web client?

Why is that happen and what is wrong?

Thank you.

This is my webserver code, very simple:

    import socket
    
    HOST = '192.168.43.157'  
    PORT = 80
    
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    print('# Socket created')
    # Create socket on port
    try:
        s.bind((HOST, PORT))
    except socket.error as msg:
        print('# Bind failed. ')
        sys.exit()
    
    print('# Socket bind complete')
    
    # Start listening on socket
    s.listen(10)
    print('# Socket now listening')
    
    # Wait for client
    conn, addr = s.accept()
    print('# Connected to ' + addr[0] + ':' + str(addr[1]))
    response = open(r"C:\Users\Totz Tech\Videos\response.txt",'rb').read()
    
    # Receive data from client
    while True:
        print (addr[0])
        data = conn.recv(5048)
        line = data.decode('UTF-8') # convert to string (Python 3 only)
        #Parse the line to get the web page request
        line_member = line.split("\r\n")   # remove newline character
        line0member = line_member[0]
        line01member = line0member.split(" ")
        try:
            line01member[1][1:] == ""
        except:
            print("Ada error bro")
        else:
            if line01member[1][1:] == "":
                token = r"C:\Users\Totz Tech\Videos\mypage.html"
                print("Ini token: " + token)
            else:
                token = line01member[1][1:]
                print("Ini token: " + token)
        
        f1 = open(token,'rb')
        f1r = f1.read()
        conn.send(response+f1r)
        print("Go UP")
         

s.close()

This is the reponse.txt file contain:

HTTP/1.1 200 OK

This is the mywebpage.html file contain:

<!DOCTYPE html>
<html>
<body>

<h1>The a download attribute</h1>

<p>Click on the image to download it:<p>
<a href="w3logo.jpg" download><img src="w3logo.jpg"></img></a>


<p>Notice that the filename of the downloaded file will be saved as "w3logo.jpg" instead of "myw3schoolsimage.jpg".</p>

<p><b>Note:</b> The download attribute is not supported in IE or Edge (prior version 18), or in Safari (prior version 10.1).</p>

</body>
</html>

This is Wireshark image of the second web client request:

The Mr. Totardo
  • 1,119
  • 2
  • 10
  • 11
  • It is unclear what response.txt and mypage.html contain - so one cannot reproduce your problem. It is unclear what was actually captured on the wire - so one only has your interpretation which is missing details. But my speculation is that the second request comes on another TCP connection - but your code does not expect this. – Steffen Ullrich Aug 22 '23 at 14:36
  • I will add to the post what is response.txt, mypage.html and wireshark caputured print sceen image. – The Mr. Totardo Aug 22 '23 at 14:48
  • A screenshot of the packet capture with only the details of some selected part shown is not useful. Please provide the actual pcap file, ideally restricted to only the actual connections (and not unrelated traffic like currently shown). Based on the few information shown in the screenshot it looks like that there are multiple TCP connections involved (different source port), which supports my speculation. – Steffen Ullrich Aug 22 '23 at 15:02
  • Based on the `seq=1` in the screenshot it is clear that the image is requested with a new TCP connection - but your code expects only a single connection for all requests. Therefore marked as duplicate as similar questions. – Steffen Ullrich Aug 22 '23 at 15:32
  • Thank you for pointing me that. I have been looking for this answer in two days. Salute to you. Great day. Once more, thank you. – The Mr. Totardo Aug 22 '23 at 19:58

0 Answers0