- 主题:如何自己开一个http服务器并通知别人来访问?
【 在 webhost 的大作中提到: 】
: 通知对方的那个服务,不断轮询访问http服务(模拟客户发通知消息),一旦发现http
: 服务已经ready了,就给客户发消息。
: 等于把杂事垃圾实现管控在自己内部,提供对外的接口,看起来是优雅的。
这句话很精妙,学习了
: 不能让老板亲自不停地来看会议室是否打扫好了,而是由秘书盯着,打扫好了,再通知
: 老板可以来开会了。
--
FROM 139.227.19.*
gunicorn那边怎么弄呢?
这个函数其实就是onServerReady事件吧。只是名字写的不那么明显
【 在 hgoldfish 的大作中提到: 】
: 不是继承 TcpServer,而是继承你现在使用的服务器的类型。比如 flask,就会有自己
: 的 Server 类和 RequestHandler 类。
: 很多服务器,只实现了 RequestHandler,这时你确实是继承 TcpServer.
--
FROM 139.227.19.*
这可能是最好的。因为你这个 service 的 stack 可能很深,比方可能还在 proxy 后面,或者跑在 k8s 上,这样你自己这个 ready 不是 end to end 的,很容易出问题。
【 在 JulyClyde 的大作中提到: 】
: 这太腾讯了,丢不起那个人……
--
FROM 36.44.219.*
真走火入魔
搜到的代码
import http.server
import socketserver
PORT = 8080
Handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer(("", PORT), Handler) as httpd:
### 写通知
httpd.serve_forever()
【 在 JulyClyde 的大作中提到: 】
: 标 题: Re: 如何自己开一个http服务器并通知别人来访问?
: 发信站: 水木社区 (Tue Jun 18 13:30:18 2024), 转信
:
: class BaseServer:
: Methods that may be overridden:
: - server_activate()
:
: 是指这个吗?
: 所以我只要继承TCPServer,覆盖这个函数,然后用它和handler组合一下做出新的server来对外服务就可以了?
: 【 在 hgoldfish 的大作中提到: 】
: : 在 serve_forever() 之前,Python 的 socketserver 会调用 server_activate() 这个方法来 listen(),你可以在这个方法里面,自己手动调一下 listen(),把自己获得的服务器地址从这里发出去。
:
: --
:
: ※ 来源:·水木社区 mysmth.net·[FROM: 139.227.19.*]
--
FROM 61.149.143.*
【 在 vwx 的大作中提到: 】
: 真走火入魔
: 搜到的代码
: import http.server
: import socketserver
: PORT = 8080
: Handler = http.server.SimpleHTTPRequestHandler
: with socketserver.TCPServer(("", PORT), Handler) as httpd:
: ### 写通知
: httpd.serve_forever()
这不就是我说的那种明显的错误么?
--
FROM 139.227.19.*
serve_forever在对面收通知发请求前开始工作就行
看代码似乎不用太担心:
def serve_forever(self, poll_interval=0.5):
"""Handle one request at a time until shutdown.
Polls for shutdown every poll_interval seconds. Ignores
self.timeout. If you need to do periodic tasks, do them in
another thread.
"""
self.__is_shut_down.clear()
try:
# XXX: Consider using another file descriptor or connecting to the
# socket to wake this up instead of polling. Polling reduces our
# responsiveness to a shutdown request and wastes cpu at all other
# times.
with _ServerSelector() as selector:
selector.register(self, selectors.EVENT_READ)
while not self.__shutdown_request:
ready = selector.select(poll_interval)
# bpo-35017: shutdown() called during select(), exit immediately.
if self.__shutdown_request:
break
if ready:
self._handle_request_noblock()
self.service_actions()
finally:
self.__shutdown_request = False
self.__is_shut_down.set()
【 在 JulyClyde 的大作中提到: 】
: 标 题: Re: 如何自己开一个http服务器并通知别人来访问?
: 发信站: 水木社区 (Fri Jun 21 11:54:00 2024), 转信
:
:
: 【 在 vwx 的大作中提到: 】
: : 真走火入魔
: : 搜到的代码
: : import http.server
: : import socketserver
: : PORT = 8080
: : Handler = http.server.SimpleHTTPRequestHandler
: : with socketserver.TCPServer(("", PORT), Handler) as httpd:
: : ### 写通知
: : httpd.serve_forever()
: 这不就是我说的那种明显的错误么?
:
: --
:
: ※ 来源:·水木社区 mysmth.net·[FROM: 139.227.19.*]
--
FROM 61.149.143.*
拜托你再读一遍我的提问啊
我问的是怎么在serve_forever *之后* 通知对方
【 在 vwx 的大作中提到: 】
: 标 题: Re: 如何自己开一个http服务器并通知别人来访问?
: 发信站: 水木社区 (Fri Jun 21 14:51:40 2024), 转信
:
: serve_forever在对面收通知发请求前开始工作就行
: 看代码似乎不用太担心:
:
: def serve_forever(self, poll_interval=0.5):
: """Handle one request at a time until shutdown.
:
: Polls for shutdown every poll_interval seconds. Ignores
: self.timeout. If you need to do periodic tasks, do them in
: another thread.
: """
: self.__is_shut_down.clear()
: try:
: # XXX: Consider using another file descriptor or connecting to
: the
: # socket to wake this up instead of polling. Polling reduces our
: # responsiveness to a shutdown request and wastes cpu at all
: other
: # times.
: with _ServerSelector() as selector:
: selector.register(self, selectors.EVENT_READ)
:
: while not self.__shutdown_request:
: ready = selector.select(poll_interval)
: # bpo-35017: shutdown() called during select(), exit
: immediately.
: if self.__shutdown_request:
: break
: if ready:
: self._handle_request_noblock()
:
: self.service_actions()
: finally:
: self.__shutdown_request = False
: self.__is_shut_down.set()
:
:
: 【 在 JulyClyde 的大作中提到: 】
: : 标 题: Re: 如何自己开一个http服务器并通知别人来访问?
: : 发信站: 水木社区 (Fri Jun 21 11:54:00 2024), 转信
: :
: :
: : 【 在 vwx 的大作中提到: 】
: : : 真走火入魔
: : : 搜到的代码
: : : import http.server
: : : import socketserver
: : : PORT = 8080
: : : Handler = http.server.SimpleHTTPRequestHandler
: : : with socketserver.TCPServer(("", PORT), Handler) as httpd:
: : : ### 写通知
: : : httpd.serve_forever()
: : 这不就是我说的那种明显的错误么?
: :
: : --
: :
: : ※ 来源:·水木社区 mysmth.net·[FROM: 139.227.19.*]
:
: --
: 忘记我。否则就太迟了。
:
:
: ※ 来源:·水木社区 mysmth.net·[FROM: 61.149.143.*]
--
修改:JulyClyde FROM 139.227.19.*
FROM 139.227.19.*
在进入 serve_forever() 后,整个线程已经阻塞住了。既然是阻塞住了,这时候就没法跑任何 python 代码了,除非启动个新线程。
所以如果要向客户发出通知,得在 server_activate() 这个函数里面启动新线程,在新线程里面把自己的端口号发送给客户端。在发送端口号的时候,serve_forever() 也跑了起来,可以接受新连接。
【 在 JulyClyde 的大作中提到: 】
: 拜托你再读一遍我的提问啊
: 我问的是怎么在serve_forever *之后* 通知对方
--
FROM 120.37.23.*
我很怀疑楼主是否能看懂下面的文字
【 在 hgoldfish 的大作中提到: 】
: 在进入 serve_forever() 后,整个线程已经阻塞住了。既然是阻塞住了,这时候就没法跑任何 python 代码了,除非启动个新线程。
: 所以如果要向客户发出通知,得在 server_activate() 这个函数里面启动新线程,在新线程里面把自己的端口号发送给客户端。在发送端口号的时候,serve_forever() 也跑了起来,可以接受新连接。
--
FROM 1.202.141.*
我的提问不就是这段么
【 在 kawolu 的大作中提到: 】
: 我很怀疑楼主是否能看懂下面的文字
--
FROM 139.227.19.*