Python: 运行 SimpleHTTPServer 并在脚本中向它发出请求
Python: run SimpleHTTPServer and make request to it in a script
我想编写 Python 脚本:
- 在某个端口上启动 SimpleHTTPServer
- 向服务器发出请求并通过POST
发送一些数据
- 运行 脚本通过 shell 交互模式并与之交互
现在代码如下所示:
import SimpleHTTPServer
import SocketServer
import urllib
import urllib2
# Variables
URL = 'localhost:8000'
PORT = 8000
# Setup simple sever
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "serving at port", PORT
httpd.serve_forever()
# Getting HTML from the target page
values = {
'name': 'Thomas Anderson',
'location': 'unknown'
}
data = urlilib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
html = response.read()
问题是一旦我 运行 我的脚本
python -i foo.py
它打印 serving at port 8000
然后冻结。我敢打赌这对于这里的 Python 大师来说是微不足道的,但我们将不胜感激。
运行 服务器作为 不同的进程 ,这将允许您 运行 脚本的其余部分。
我也宁愿使用 requests 而不是 urllib。
import SocketServer
import SimpleHTTPServer
import requests
import multiprocessing
# Variables
PORT = 8000
URL = 'localhost:{port}'.format(port=PORT)
# Setup simple sever
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "Serving at port", PORT
# start the server as a separate process
server_process = multiprocessing.Process(target=httpd.serve_forever)
server_process.daemon = True
server_process.start()
# Getting HTML from the target page
values = {
'name': 'Thomas Anderson',
'location': 'unknown'
}
r = requests.post(URL, data=values)
r.text
# stop the server
server_process.terminate()
或者 运行 它在单独的 thread
:
import SimpleHTTPServer
import SocketServer
import urllib2
from threading import Thread
from datetime import time
# Variables
URL = 'localhost:8000'
PORT = 8000
# Setup simple sever
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "serving at port", PORT
def simple_sever():
httpd.serve_forever()
simple_sever_T = Thread(target=simple_sever, name='simple_sever')
simple_sever_T.daemon = True
simple_sever_T.start()
while not simple_sever_T.is_alive():
time.sleep(1)
# Getting test file
req = urllib2.Request('http://localhost:%s/test_file' % PORT)
response = urllib2.urlopen(req)
print response.read()
httpd.shutdown()
我想编写 Python 脚本:
- 在某个端口上启动 SimpleHTTPServer
- 向服务器发出请求并通过POST 发送一些数据
- 运行 脚本通过 shell 交互模式并与之交互
现在代码如下所示:
import SimpleHTTPServer
import SocketServer
import urllib
import urllib2
# Variables
URL = 'localhost:8000'
PORT = 8000
# Setup simple sever
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "serving at port", PORT
httpd.serve_forever()
# Getting HTML from the target page
values = {
'name': 'Thomas Anderson',
'location': 'unknown'
}
data = urlilib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
html = response.read()
问题是一旦我 运行 我的脚本
python -i foo.py
它打印 serving at port 8000
然后冻结。我敢打赌这对于这里的 Python 大师来说是微不足道的,但我们将不胜感激。
运行 服务器作为 不同的进程 ,这将允许您 运行 脚本的其余部分。
我也宁愿使用 requests 而不是 urllib。
import SocketServer
import SimpleHTTPServer
import requests
import multiprocessing
# Variables
PORT = 8000
URL = 'localhost:{port}'.format(port=PORT)
# Setup simple sever
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "Serving at port", PORT
# start the server as a separate process
server_process = multiprocessing.Process(target=httpd.serve_forever)
server_process.daemon = True
server_process.start()
# Getting HTML from the target page
values = {
'name': 'Thomas Anderson',
'location': 'unknown'
}
r = requests.post(URL, data=values)
r.text
# stop the server
server_process.terminate()
或者 运行 它在单独的 thread
:
import SimpleHTTPServer
import SocketServer
import urllib2
from threading import Thread
from datetime import time
# Variables
URL = 'localhost:8000'
PORT = 8000
# Setup simple sever
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "serving at port", PORT
def simple_sever():
httpd.serve_forever()
simple_sever_T = Thread(target=simple_sever, name='simple_sever')
simple_sever_T.daemon = True
simple_sever_T.start()
while not simple_sever_T.is_alive():
time.sleep(1)
# Getting test file
req = urllib2.Request('http://localhost:%s/test_file' % PORT)
response = urllib2.urlopen(req)
print response.read()
httpd.shutdown()