Python:运行 SimpleHTTPServer 并在脚本中向它发出请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34932268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:43:56  来源:igfitidea点击:

Python: run SimpleHTTPServer and make request to it in a script

pythonpython-2.7serversimplehttpserver

提问by 0leg

I would like to write Python script that would:

我想编写 Python 脚本:

  1. Start SimpleHTTPServer on some port
  2. Make a request to the server and send some data via POST
  3. Run script via shell in interactive mode and interact with it
  1. 在某个端口上启动 SimpleHTTPServer
  2. 向服务器发出请求并通过 POST 发送一些数据
  3. 在交互模式下通过 shell 运行脚本并与之交互

Right now the code looks like this:

现在代码看起来是这样的:

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()

The problem is that once I run my script

问题是一旦我运行我的脚本

python -i foo.py

It prints serving at port 8000and then freezes. I bet this is something trivial for Python gurus here, but help would be appreciated.

它打印serving at port 8000然后冻结。我敢打赌,这对于 Python 大师来说是微不足道的,但我们将不胜感激。

采纳答案by Du?an Ma?ar

Run the server as a different process, that will allow you to run the rest of your script.

将服务器作为不同的进程运行,这将允许您运行脚本的其余部分。

I would also rather use requeststhan urllib.

我也宁愿使用请求而不是 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()

回答by NarūnasK

Alternatively run it in the separate thread:

或者在单独的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()