我只是尝试实现一个类似于代理的python应用程序(用于端口22)代码如下:
import socket
import select
import code
inputSock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
inputSock.bind( ('',9999) )
inputSock.listen(5)
socks = {} #a dictionary!
#Now, wait for an input!
while 1:
#accept new conns
(sread,swrite,sexec) = select.select([inputSock],[],[])
for sock in sread:
print("Got an input!")
newsock,(remhost,remport) = sock.accept()
sendsock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:
print("Now connecting to the centOS...")
sendsock.connect( ('202.131.30.12',80))
except:
print("ERROR: cannot connect to host 192.168.52.128:22")
print("successfully connected to Cent. Creating new socks")
socks[id(sendsock)] = newsock
socks[id(newsock)] = sendsock
print("socks created!")
#recv from socks, etc.
(sread,swrite,sexec) = select.select(socks.values(),[],[])
for sock in sread:
print("got input!")
data = sock.recv(700)
print(data)
if(data == ""):
#remove that sock.
id1 = id(sock)
id2 = id(socks[id1])
socks.pop(id1)
socks.pop(id2)
print("removed empty conns")
else:
#code.interact(globals=globals())
sendSock = socks[id(sock)]
sendSock.send(data)
print("data sent!")
现在,当我运行脚本并将浏览器指向 127.0.0.1:9999 时,我的浏览器将无法获取网页(处于某种“永久加载网页”状态)。这就是我从 python 提示符得到的:http: //imgur.com/CffOY
你能帮我找出哪里出错并修复这个实验性实现吗?(或者给我一些链接以获得一个想法?)谢谢 :)