2017-11-14 14 views
0

私は私たちの一部を編集する必要があるプログラムを割り当てられましたが、私の人生にとってはまずそれが働くことはありません。私はちょうど私が愚かであることを私が知っているので、誰かが私を助けてくれることを望んでいた。私のproffesorからこのプログラムの仕組みを理解するのに助けが必要です

私はそれがWebブラウザを介して動作することを知っていますが、私はちょうどそれをどのように入力するか、そのようなものは表示されません。

# ------------------------------------------------------------------------------- 
# Name:  Hello World Server 
# Purpose:  ¯\_(ツ)_/¯ 
# 
# Author:  JaredRand 
# 
# Created:  11/1/17 
# Copyright: 
# Licence: 
# ------------------------------------------------------------------------------- 

from socket import * 


def main(): 
    serverPort = 8080 
    serverSocket = socket(AF_INET, SOCK_STREAM) 
    serverSocket.bind(('localhost', serverPort)) 
    serverSocket.listen(0) # number of backlogged connections 
    print('server ready') 
    while 1: 

     try: 
      connectionSocket, addr = serverSocket.accept() # make everything after this a function? 
      print('passed try 1') 
     except IOError: 
      print("Server Socket Accept Error") 

     try: 
      request = connectionSocket.recv(1024).decode('utf-8') 
      print('passed try 2') 
      print(request) 
     except IOError: 
      print("Server Socket Recv Error") 

     if request: 
      # https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html 
      try: 

       [Method, Request_URI, HHTP_Version] = request.split(' ', 2) 
       print('passed try 3') 
       print(Method) 
       print(Request_URI) 
       print(HHTP_Version) 
      except ValueError: 
       print("Request Parse Error:" + request) 

      try: 
       # https://www.ietf.org/rfc/rfc2396.txt 
       [scheme, hier_part] = Request_URI.split(":", 1) 
       print('passed try 4') 
       print(scheme) 
       print(hier_part) 
      except ValueError: 
       print("No Scheme") 
       scheme = None 
       hier_part = Request_URI 

      # more parsing is required but assuming the Request_URI is a path 
      print("Request URI is: " + hier_part) 

      # see if the file is present 
      if hier_part != "/": 
       try: 
        print("Request File is: " + hier_part) 
        fo = open('.' + hier_part, "rb") 
       except IOError: 
        # here need to send a 404 error 
        http_status = 'HTTP/1.1 404 Not Found\n' 
        http_content = 'Content-Type: text/html charset=utf-8\n\n' 
        outputdata = 'Bad File' 
       else: 
        # right now only file we have is the icon 
        outputdata = fo.read() 
        fo.close() 
        http_status = 'HTTP/1.1 200 OK\n' 
        http_content = 'Content-Type: image/x-icon\n\n' 
      else: 
       # here we would the contents of index.html 
       outputdata = '<!DOCTYPE html><head><meta charset="utf-8">' \ 
          + '<title> test </title></head><body><h1>Index File</h1><p>Should be index</p></body></html>' 
       http_status = 'HTTP/1.1 200 OK\n' 
       http_content = 'Content-Type: text/html charset=utf-8\n\n' 

      # send the response header 

      connectionSocket.send(http_status.encode('utf-8')) 
      connectionSocket.send('Connection: close\n'.encode('utf-8')) 
      # https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html Should 
      LengthString = 'Content-Length: ' + str(len(outputdata)) + '\n' 
      # connectionSocket.send('Transfer-Encoding: identity\n') 
      connectionSocket.send(LengthString.encode('utf-8')) 
      connectionSocket.send(http_content.encode('utf-8')) 

      print(type(outputdata)) 
      try: 
       outputdatae = outputdata.encode('utf-8') 
      except AttributeError: 
       outputdatae = outputdata 

      connectionSocket.send(outputdatae) 

      connectionSocket.shutdown(SHUT_RDWR) 
      connectionSocket.close() 
     else: 
      print("No Request") 

    pass 


if __name__ == '__main__': 
    main() 
+0

'python filename.py'を使用してプログラムを実行し、ブラウザウィンドウにlocalhost:8080と入力します。 –

答えて

1

これは、Webサーバーを起動するPythonスクリプトです。

  1. お使いのオペレーティングシステム用のPythonをインストールします(必要な場合)
  2. 保存webserver.py
  3. などのファイルを実行Pythonスクリプトpython webserver.py
  4. Webブラウザを開き、​​
0

それをに行きます正常に動作し、プログラムを実行してからthe portに移動してください。This site

+0

私はおそらく、私がプログラムで得ることができる限り、特定のファイル(favicon.icoと呼ばれる)をアップロードするためにファイルパスを送信することについて何かを明らかにしているはずですが、その作業を行う方法を理解できません。インデックスファイルはインデックスビットでなければなりません。他の何も動作しないときのデフォルトの出力ですが、プログラムの実行中にファイルパスを入力する方法、またはアップロードするアイコンを取得する方法がわかりません。 – MilkToast

関連する問題