Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

How can I send a file using to a HTTP server and read it?

So, I created the following HTTP server tunneled via ngrok, and I am trying to send a file to the server, to then read it and display it on the web page of the server. Here's the code for the server:

import os

from http.server import HTTPServer, BaseHTTPRequestHandler
from pyngrok import ngrok
import time

port = os.environ.get("PORT", 80)

server_address = ("127.0.0.1", port)

class MyServer(BaseHTTPRequestHandler):
    def _set_headers(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()

    def do_GET(self):
        self._set_headers()
        self.wfile.write(bytes("<html><head><title>https://pythonbasics.org</title></head>", "utf-8"))
        self.wfile.write(bytes("<p>Request: %s</p>" % self.path, "utf-8"))
        self.wfile.write(bytes("<body>", "utf-8"))
        self.wfile.write(bytes("<p>This is an example web server.</p>", "utf-8"))
        self.wfile.write(bytes("</body></html>", "utf-8"))

    def do_POST(self):
        '''Reads post request body'''
        self._set_headers()
        content_len = int(self.headers.getheader('content-length', 0))
        post_body = self.rfile.read(content_len)
        self.wfile.write("received post request:<br>{}".format(post_body))
    
    def do_PUT(self):
        self.do_POST()

httpd = HTTPServer(server_address, MyServer)

public_url = ngrok.connect(port).public_url
print("ngrok tunnel "{}" -> "http://127.0.0.1:{}"".format(public_url, port))

try:
    # Block until CTRL-C or some other terminating event
    httpd.serve_forever()
except KeyboardInterrupt:
   print(" Shutting down server.")

   httpd.socket.close()

And I have been trying to send a file using POST as follow

>>> url = 'https://httpbin.org/post'
>>> files = {'file': open('report.xls', 'rb')}

>>> r = requests.post(url, files=files)
>>> r.text

I imported requests of course, and here's what I get

Exception occurred during processing of request from ('127.0.0.1', 60603)
Traceback (most recent call last):
  File "C:Program FilesPython39libsocketserver.py", line 316, in _handle_request_noblock
    self.process_request(request, client_address)
  File "C:Program FilesPython39libsocketserver.py", line 347, in process_request
    self.finish_request(request, client_address)
  File "C:Program FilesPython39libsocketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:Program FilesPython39libsocketserver.py", line 720, in __init__
    self.handle()
  File "C:Program FilesPython39libhttpserver.py", line 427, in handle
    self.handle_one_request()
  File "C:Program FilesPython39libhttpserver.py", line 415, in handle_one_request
    method()
  File "C:UserspierrOneDriveDesktopSpyWare-20210104T124335Z-001SpyWareNgrok_Test.py", line 28, in do_POST
    content_len = int(self.headers.getheader('content-length', 0))
AttributeError: 'HTTPMessage' object has no attribute 'getheader'

Could someone please help me fix this error ? I don't get where it comes from.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...