1

HTTP CLIENT がサーバーに対して同時に 100 の REQUEST を作成する方法を知りたいのですが、要求ごとに異なる IP アドレスまたは IP アドレスの範囲を使用して、 Pythonでコードを作成するか、異なる IP アドレスまたは IP の範囲を構成してコードで CURL 関数を使用します。住所

サーバー上のHtmlまたはWebページを要求し、その内容を読み取る別のIPアドレスを使用して、サーバーにいくつかの要求を行うようにコードを編集するのを手伝ってください。

幸いなことに、Python は BaseHTTPServer と呼ばれる HTTP サーバー モジュールを提供します。このモジュールの 2 つのクラス、つまり BaseHTTPRequestHandler と HTTPServer を使用します。また、システム内のファイルにアクセスするには、os モジュールを使用する必要があります。

まず、これらのモジュールをインポートする必要があります

#!/usr/bin/env python



from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import os

次に、BaseHTTPRequestHandler を実装するカスタム クラス、つまり KodeFunHTTPRequestHandler を作成します。

#Create custom HTTPRequestHandler class
class KodeFunHTTPRequestHandler(BaseHTTPRequestHandler):

HTTP から GET コマンドを実装するだけです。そのためには、do_GET() 関数をオーバーライドする必要があります。私たちの目的は、クライアントを使用してサーバーから HTML ファイルを要求することです (次のステップで説明します)。do_GET() 関数内のスクリプトは、目的を処理するために記述されています

#handle GET command
def do_GET(self):
    rootdir = 'c:/xampp/htdocs/' #file location
    try:
        if self.path.endswith('.html'):
            f = open(rootdir + self.path) #open requested file


        #send code 200 response
        self.send_response(200)

        #send header first
        self.send_header('Content-type','text-html')
        self.end_headers()

        #send file content to client
        self.wfile.write(f.read())
        f.close()
        return

except IOError:
    self.send_error(404, 'file not found')

最後に、次のスクリプトを追加してサーバーを実行します

def run():
    print('http server is starting...')


#ip and port of servr
#by default http server port is 80
server_address = ('127.0.0.1', 80)
httpd = HTTPServer(server_address, KodeFunHTTPRequestHandler)
print('http server is running...')
httpd.serve_forever()

if __name__ == '__main__':
    run()

保存して、好きな名前を付けます (例: kodefun-httpserver.py)。http サーバーの完全なスクリプトは次のとおりです。

kodefun-httpserver.py


#!/usr/bin/env python

from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import os

#Create custom HTTPRequestHandler class
class KodeFunHTTPRequestHandler(BaseHTTPRequestHandler):

        #handle GET command
        def do_GET(self):
                rootdir = 'c:/xampp/htdocs/' #file location
                try:
                        if self.path.endswith('.html'):
                                f = open(rootdir + self.path) #open requested file

                                #send code 200 response
                                self.send_response(200)

                                #send header first
                                self.send_header('Content-type','text-html')
                                self.end_headers()

                                #send file content to client
                                self.wfile.write(f.read())
                                f.close()
                                return

                except IOError:
                        self.send_error(404, 'file not found')

def run():
print('http server is starting...')

#ip and port of servr
#by default http server port is 80
server_address = ('127.0.0.1', 80)
httpd = HTTPServer(server_address, KodeFunHTTPRequestHandler)
print('http server is running...')
httpd.serve_forever()

if __name__ == '__main__':
    run()

単純な HTTP クライアントを作成する

サーバーが正常に動作しているかどうかを確認するには、HTTP クライアントが必要です。Web ブラウザを使用してこれを行うことができます。でも、自分のクライアントを作ればカッコイイですよね。これを実現するために、別のスクリプトを作成して、kodefun-httpclient.py という名前を付けましょう。次のスクリプトをファイルにコピーします: kodefun-httpclient.py

#!/usr/bin/env python (CLIENT CODE)

import httplib
import sys

#get http server ip
http_server = sys.argv[1]
#create a connection
conn = httplib.HTTPConnection(http_server)

while 1:
            cmd = raw_input('input command (ex. GET index.html): ')
            cmd = cmd.split()

            if cmd[0] == 'exit': #tipe exit to end it
                        break

            #request command to server
            conn.request(cmd[0], cmd[1])


            #get response from server
            rsp = conn.getresponse()

            #print server response and data
            print(rsp.status, rsp.reason)
            data_received = rsp.read()
            print(data_received)

conn.close

GET コマンドを使用してテストする

4

0 に答える 0