我使用vagrant ubuntu服务器(16.04):127.0.0.1,端口是2222用于开发Web应用程序,测试代码(app.py)如下:
import logging; logging.basicConfig(level=logging.INFO)
import asyncio, os, json, time
from datetime import datetime
from aiohttp import web
def index(request):
return web.Response(body=b'<h1>Awesome</h1>')
@asyncio.coroutine
def init(loop):
app = web.Application(loop=loop)
app.router.add_route('GET', '/', index)
srv = yield from loop.create_server(app.make_handler(), '127.0.0.1', 2222)
logging.info('server started at http://127.0.0.1:9000...')
return srv
loop = asyncio.get_event_loop()
loop.run_until_complete(init(loop))
loop.run_forever()
然后我想在浏览器中测试应用程序,但没有回应!和错误:
答案 0 :(得分:1)
问题是服务器正在侦听VM上的IP 127.0.0.1,因此您将无法从主机访问。
如果要从主机浏览器访问它,则需要在专用IP或import scrapy
class OpendotaSpider(scrapy.Spider):
# name der Spinne
name = "opendota"
start_urls = ['https://www.opendota.com/matches/3607703223']
def parse(self, response):
for post in response.css('body'):
yield {
# [0].extract() = extract_first()
'Winner': post.css('.matchinfo span ::text').extract_first(),
'Matchid': post.css('.additionalInfo li ::text').extract_first(),
'Duration': post.css('.duration span ::text').extract()
}
IP上运行服务器,以便更改为
0.0.0.0
然后确保从您的Vagrantfile转发此端口:
srv = yield from loop.create_server(app.make_handler(), '127.0.0.1', 9000)
您将能够通过主持人config.vm.network "forwarded_port", guest: 9000, host: 9000
访问它
答案 1 :(得分:0)
默认2222
端口来自主机ssh的转发22
..您确定port_forwarded
中的Vagrantfile
设置是否正确..?还要检入如果您要打开的端口是使用sudo netstat -ntlp
打开的,那么您是主机,通常流浪端口的PID将为VBOXHeadless