python localhost_简单的python localhost代理 – 几乎正常工作

我正在开发一个稍微大一点的项目,我需要在

python中创建一个localhost代理.

我写的方式是在localhost上的端口8080上有一个TCP服务器(使用套接字和SOCK_STREAM).它接受来自本地主机的请求,使用切片,string.find()和gethostbyname()查找目标IP,因此它打开另一个TCP套接字,发送请求并回复一个回复.之后,它将回复中继回localhost代理,后者又将其抛回浏览器.

这是具有充足调试消息和调试文件的代码,用于收集浏览器的请求和收到的回复(还要注意这只是一个原型,因此限制for循环而不是while 1循环):

import socket

local = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

f = open('test.txt', 'a')

local.bind(('localhost', 8080))

local.listen(5)

for i in xrange(20):

print '=====%d=====\n' % i

out = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

data, addr = local.accept()

print 'Connection accepted'

buffer = data.recv(4096)

print 'data recieved'

f.write('=============================================================\n')

f.write(buffer)

end = buffer.find('\n')

print buffer

#print buffer[:end]

host = buffer[:end].split()[1]

end = host[7:].find('/')

print host[7:(end+7)]

host_ip = socket.gethostbyname(host[7:(end+7)])

#print 'remote host: ' + host + ' IP: ' + host_ip

print 'sending buffer to remote host'

out.connect((host_ip, 80))

out.sendall(buffer)

print 'recieving data from remote host'

reply = out.recv(4096)

out.close()

print 'data recieved from remote host'

f.write('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n')

f.write(reply)

f.write('\n\n\n')

print 'sending data back to local host'

data.sendall(reply)

print 'data sent'

local.close()

out.close()

f.close()

现在我的问题是它似乎对前几个请求工作正常,它获取html和一些图像,但在某些时候它总是停在“数据接收”点并退出,因为它没有数据即.缓冲区为空.浏览器仍然显示它正在加载页面的元素,但是当它停止并且我查看文本日志文件时,我看到缓冲区是空的,这意味着浏览器没有向代理提交任何内容?

我猜这个问题在于浏览器如何提交请求以及我的脚本没有对此行为做出正确反应.

我知道我可以使用Twist框架,但是我想学习自己写这种东西.我一直在阅读有关SocketServer的内容,我可能会使用它,但我不知道它是否会解决这个问题,因为坦率地说,我真的不明白是什么导致了这个问题.我的脚本对于浏览器来说太慢了吗?服务器是否发送了多个答案,而我的接收套接字应该监听更多数据包?我的缓冲区大小(4096)太小了吗?

我真的很感激在正确的方向上轻推.

谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Confluent Kafka is a Python client library for Apache Kafka, developed by Confluent. It provides an easy-to-use interface for interacting with Kafka clusters, allowing you to produce and consume messages from Kafka. To use the confluent_kafka library in, you first need to install it. You can do this by running the following command: ``` pip install confluent-kafka ``` Once installed, you can import the library in your Python code as follows: ```python from confluent_kafka import Producer, Consumer ``` To produce messages to a Kafka topic, you can create a `Producer` instance and use its `produce()` method. Here's an example: ```python producer = Producer({'bootstrap.servers': 'localhost:9092'}) topic = 'my_topic' message = 'Hello, Kafka!' producer.produce(topic, message.encode('utf-8')) producer.flush() ``` To consume messages from a Kafka topic, you can create a `Consumer` instance and use its `subscribe()` and `poll()` methods. Here's an example: ```python consumer = Consumer({ 'bootstrap.servers': 'localhost:9092', 'group.id': 'my_consumer_group', 'auto.offset.reset': 'earliest' }) topic = 'my_topic' consumer.subscribe([topic]) while True: msg = consumer.poll(1.0) if msg is None: continue if msg.error(): print(f"Consumer error: {msg.error()}") continue print(f"Received message: {msg.value().decode('utf-8')}") consumer.close() ``` These are just basic examples to get you started with the confluent_kafka library. You can refer to the official documentation for more advanced usage and configuration options. Please note that you need a running Kafka cluster to use the confluent_kafka library.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值