Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[HELP] Unable to share state between the main app and a plugin #1468

Unanswered
ProtocolNebula asked this question in Q&A
Discussion options

Hi,

I'm trying to do a plugin to do a "rotate proxy", but the next IP must be rotated by the main app, not the plugin itself.

I have the following code:

import proxy
from proxy.http.proxy import HttpProxyBasePlugin
class CustomProxyRedirection(HttpProxyBasePlugin):
 currentProxy = {
 "address": "0.0.0.0"
 }
 
 def __init__(self, *args, **kwargs):
 super().__init__(*args, **kwargs)
 
 def before_upstream_connection(self, request):
 print(f'Proxy host: {CustomProxyRedirection.currentProxy['address']}')
 proxy_host, proxy_port = CustomProxyRedirection.currentProxy['address'].split(':')
 # Set the proxy for the request
 request.host = proxy_host
 request.port = int(proxy_port)
 return request
class ProxyManager:
 proxy = None
 def __init__(self):
 self.proxy = proxy.Proxy(port=8899, plugins=[
 CustomProxyRedirection,
 ])
 def nextProxy(self):
 CustomProxyRedirection.currentProxy['address'] ="154.94.5.241:7001"
 # EXTREME CASE WHERE I INSTANTIATE THE WHOLE PROXY AGAIN
 self.proxy = proxy.Proxy(port=8899, plugins=[
 CustomProxyRedirection,
 ])
# Example to run the proxy
def main():
 proxyManager = ProxyManager()
 proxyManager.nextProxy()
 with proxyManager.proxy as p:
 logger.info('Application started')
 proxy.sleep_loop()
if __name__ == '__main__':
 main()

I've tried different solutions, even I tried to understand how to use the multiprocess manager, but I found no way to do it.

Someone can please help me with an example or an explanation of how can I do it? (I assume that I have to spawn by myself the proxy, but I'm too new to python and found no clues/information of how to manage this case).

Thanks.

You must be logged in to vote

Replies: 1 comment

Comment options

  1. If multiprocessing state sharing feels tricky, simply use an external storage like file on Disk, Sqlite, Memcache, Redis etc for sharing states.
  2. You may use Python's inbuilt multiprocessing shared variables for the same too
You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
Question Questions related to proxy server
Converted from issue

This discussion was converted from issue #1467 on August 28, 2024 02:59.

AltStyle によって変換されたページ (->オリジナル) /