【问题标题】:Python: run SimpleHTTPServer and make request to it in a scriptPython:运行 SimpleHTTPServer 并在脚本中向它发出请求
【发布时间】:2016-04-28 04:49:35
【问题描述】:

我想编写 Python 脚本:

  1. 在某个端口上启动 SimpleHTTPServer
  2. 向服务器发出请求并通过 POST 发送一些数据
  3. 在交互模式下通过 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 专家来说是微不足道的,但我们将不胜感激。

【问题讨论】:

    标签: python python-2.7 server simplehttpserver


    【解决方案1】:

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

    我也宁愿使用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()
    

    【讨论】:

    • 谢谢,这是我一直在寻找的,但由于python-experience限制无法获得。
    • 我注意到的一件事是,尝试执行此操作时出错:socket.error: [Errno 10048] 每个套接字地址(协议/网络地址/端口)通常只允许使用一次泰德
    • 这很可能是因为您没有关闭 http 服务器,至少在您的示例中是这样。详情请看我的回答。
    • 您尝试重新启动脚本时是否收到此错误?所以是的,正如@NarūnasK 建议的那样,服务器可能仍在运行,因此端口8000 被占用。答案已编辑。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-29
      • 1970-01-01
      • 2020-01-18
      • 2017-10-25
      • 1970-01-01
      • 2020-02-08
      • 1970-01-01
      • 2021-10-31
      相关资源
      最近更新 更多