iT邦幫忙

0

python http server TCP問題

  • 分享至 

  • xImage

問題如下
https://ithelp.ithome.com.tw/upload/images/20220804/20151055v99AiOL7en.jpg
如果圖片不清楚 這邊附上imgur https://i.imgur.com/X6UIsRv.jpg
.2.34是server
.2.100是android
.2.214是iPhone 13(ios 15)
想問問大神們為什麼python會比android多給ios一個ACK
給完過了30秒才又把剩下的封包傳完

介紹一下環境
網頁的前端是js在互動的
python這邊接收到的都是html或js發來的request
js是建立在mithril.js上的

以下是python的程式碼

# -*- coding: utf-8 -*-

filepos = "E:/code/es/web_ncgi/v3.4"

import sys
import http.server
import socket
from http.server import BaseHTTPRequestHandler, HTTPServer
import logging
import requests 

class S(BaseHTTPRequestHandler):
	def _set_response(self):
		self.send_response(200)
		self.send_header('Content-type', 'text/html')
		self.end_headers()
	
	def do_GET(self):
		logging.info("  GET : %s", str(self.path))
		logging.info(self)
		if(self.path == "/index.html"):
			self.send_response(200)
			self.send_header("Content-type", "text/html")
			self.end_headers()
			file = open(filepos + "/index.html", "r", encoding="utf-8")
			self.wfile.write(bytes(file.read(), "utf-8"))
		elif(self.path.find(".js") != -1):
			self.send_response(200)
			self.send_header("Content-type", "text/javascript")
			self.end_headers()
			file = open(filepos + self.path, "r", encoding="utf-8")
			self.wfile.write(bytes(file.read(), "utf-8"))
		elif(self.path.find(".css") != -1):
			self.send_response(200)
			self.send_header("Content-type", "text/css")
			self.end_headers()
			file = open(filepos + self.path, "r", encoding="utf-8")
			self.wfile.write(bytes(file.read(), "utf-8"))
		elif(self.path.find(".png") != -1):
			self.send_response(200)
			self.send_header("Content-type", "image/png")
			self.end_headers()
			file = open(filepos + self.path, "rb")
			self.wfile.write(file.read())
		elif(self.path.find(".jpg") != -1):
			self.send_response(200)
			self.send_header("Content-type", "image/jpg")
			self.end_headers()
			file = open(filepos + self.path, "rb")
			self.wfile.write(file.read())
		elif(self.path.find(".ico") != -1):
			self.send_response(200)
			self.send_header("Content-type", "image/x-icon")
			self.end_headers()
			file = open(filepos + self.path, "rb")
			self.wfile.write(file.read())
		elif(self.path.find(".cgi") != -1):
			try :
				self.send_response(200)
				self.send_header("Content-type", "application/json")
				self.end_headers()
				
				r = requests.get('http://192.168.2.83:80' + str(self.path), headers = self.headers)
				#logging.info(r.content)
				self.wfile.write(r.content)
				
			except requests.exceptions.ConnectionError:
				logging.info("request failed\n")
				self.send_response(400)
				
	def do_POST(self):
		#logging.info("POST :\n %s", self.)
		content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
		post_data = self.rfile.read(content_length) # <--- Gets the data itself
		#logging.info("POST request,\nPath: %s\nHeaders:\n%s\n\nBody:\n%s\n",
        #        str(self.path), str(self.headers), post_data.decode('utf-8'))

		try :
			self.send_response(200)
			self.send_header("Content-type", "application/json")
			self.end_headers()
			
			r = requests.post('http://192.168.2.83:80' + str(self.path), headers = self.headers, data = post_data)
			self.wfile.write(r.content)
			logging.info(r.content)
		except requests.exceptions.ConnectionError:
			logging.info("request failed\n")
			self.send_response(400)
		
def run(server_class=HTTPServer, handler_class=S, port=25472):
    logging.basicConfig(level=logging.INFO)
    server_address = ('', port)
    httpd = server_class(server_address, handler_class)
    logging.info('Starting httpd...\n')
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        pass
    httpd.server_close()
    logging.info('Stopping httpd...\n')

if __name__ == '__main__':
    from sys import argv

    if len(argv) == 2:
        run(port=int(argv[1]))
    else:
        run()
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答