问题描述
我正在尝试测量在Ubuntu VM中的Apache中运行的非常简单的服务的响应时间。该服务由HTML表单组成,该表单将发出POST请求,并使用CGI脚本进行应答。 CGI脚本只需要输入2个整数,然后将它们相加,然后返回带有结果的字符串。
通过浏览器访问VM,单击“表单”按钮后,结果将显示在新选项卡中。使用Wireshark可以很容易地获得答复时间,但是,我要做的是创建一个脚本,该脚本将发送一批请求并在一定的置信区间内获得平均值。我正在尝试使用请求库在Python中制作脚本。
我的问题如下:当我在测量脚本中使用post方法时,我没有得到答复,但是包含表单的初始网页。尽管post方法具有allow_redirects选项默认情况下设置为true。因此,我不是在评估我的服务的响应时间,而是在只是获取表格的响应时间。我该怎么办才能解决这个问题?
我在下面提供了一个最小的可复制示例,只需按照以下步骤操作即可。
1-使用以下命令创建master_script.sh文件:
#!/bin/bash
#install apache2 and python2.x in Debian based VM
sudo apt-get update
sudo apt-get install apache2 python
#setting up virtual hosts
sudo mkdir /var/www/simple_server
sudo chown -R $USER:$USER /var/www/simple_server
sudo chmod -R 755 /var/www/simple_server
#copy webpage
cp index.html /var/www/simple_server
#copy virtual host conf file and enable it,disable default
sudo cp simple_server.conf /etc/apache2/sites-available
sudo a2ensite simple_server.conf
sudo a2dissite 000-default.conf
#copy cgi python script
sudo mkdir /var/www/simple_server/scripts
sudo chown -R $USER:$USER /var/www/simple_server/scripts
sudo chmod -R 755 /var/www/simple_server/scripts
sudo cp post_reply.py /var/www/simple_server/scripts
sudo chown -R $USER:$USER /var/www/simple_server/scripts/post_reply.py
sudo chmod -R 755 /var/www/simple_server/scripts/post_reply.py
#enable cgi
sudo a2enmod cgi
2-使用以下命令创建simple_server.conf文件:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName simple_server
ServerAlias simple_server
DocumentRoot /var/www/simple_server
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
3-使用以下命令创建index.html文件:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Simple CGI Server!</title>
</head>
<body>
<h1>Simple CGI Server</h1>
<h2>Input 2 numbers to be added</h2>
<form action="scripts/post_reply.py" method="post" target="_blank">
Operand 1: <input type="text" name="operand1"><br>
Operand 2: <input type="text" name="operand2"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
#!/usr/bin/python
# Import modules for CGI handling
import cgi,cgitb
# Create instance of FieldStorage
form = cgi.FieldStorage()
#TEST
if "operand1" not in form or "operand2" not in form:
print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>CGI</title>"
print "</head>"
print "<body>"
print "<h1>Error</h1>"
print "There is some problem with post..."
print "</body>"
print "</html>"
else:
# Get data from fields
op1 = int(form.getvalue('operand1'))
op2 = int(form.getvalue('operand2'))
sum = op1 + op2
print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>CGI</title>"
print "</head>"
print "<body>"
print "<h2>The numbers add to %d</h2>" % sum
print "</body>"
print "</html>"
5-将4个文件放在同一目录中并运行主脚本。
6-将以下内容添加到/ etc / apache2中的apache2.conf中:
<Directory /var/www/simple_server/scripts>
Options +ExecCGI
AddHandler cgi-script .py
</Directory>
7-重新启动apache
sudo systemctl restart apache2
8-服务器现在可以运行-通过在浏览器中输入VM的IP对其进行测试
9-最后,使用测量脚本test.py
访问服务器#!/usr/bin/python
import time
import requests
url = 'http://{THE-VM'S-IP-GOES-IN-HERE}'
myobj = {'operand1': 7,'operand2': 5}
initial_time = time.clock()
x = requests.post(url,data = myobj)
final_time = time.clock() - initial_time
print(x.text)
print(final_time)
解决方法
只需使用CGI脚本的URL而不是索引页面的URL即可解决问题。
在测量脚本(第9步)上进行更改:
[-1.0,1.0]
收件人:
url = 'http://{THE-VM'S-IP-GOES-IN-HERE}'