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>