我正在尝试将以下Python 2代码转换为Python 3。这段Python代码用于TCP端口转发。它来自于这个页面:http://code.activestate.com/recipes/483730-port-forwarding/
import socket
import sys
import thread
def main(setup, error):
sys.stderr = file(error, 'a')
for settings in parse(setup):
thread.start_new_thread(server, settings)
lock = thread.allocate_lock()
lock.acquire()
lock.acquire()
def parse(setup):
settings = list()
for line in file(setup):
parts = line.split()
settings.append((parts[0], int(parts[1]), int(parts[2])))
return settings
def server(*settings):
try:
dock_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
dock_socket.bind(('', settings[2]))
dock_socket.listen(5)
while True:
client_socket = dock_socket.accept()[0]
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.connect((settings[0], settings[1]))
thread.start_new_thread(forward, (client_socket, server_socket))
thread.start_new_thread(forward, (server_socket, client_socket))
finally:
thread.start_new_thread(server, settings)
def forward(source, destination):
string = ' '
while string:
string = source.recv(1024)
if string:
destination.sendall(string)
else:
source.shutdown(socket.SHUT_RD)
destination.shutdown(socket.SHUT_WR)
if __name__ == '__main__':
main('proxy.ini', 'error.log')
这是我拥有的:
import socket
import sys
import threading
import time
def main(setup, error):
# open file for error messages
sys.stderr = open(error, 'a')
# read settings for port forwarding
threads = []
for settings in parse(setup):
#thread.start_new_thread(server, settings)
t = threading.Thread(target=server, args=(settings))
t.start()
threads.append(t)
for t in threads:
t.join()
# wait for <ctrl-c>
while True:
time.sleep(60)
def parse(setup):
settings = list()
file = open(setup)
for line in iter(file):
parts = line.split()
settings.append((int(parts[0]), parts[1], int(parts[2])))
return settings
def server(*settings):
try:
dock_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
dock_socket.bind(('', settings[0]))
dock_socket.listen(5)
while True:
client_socket = dock_socket.accept()[0]
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.connect((settings[1], settings[2]))
Thread1 = threading.Thread(target=forward, args=(client_socket, server_socket))
Thread1.start()
Thread2 = threading.Thread(target=forward, args=(server_socket, client_socket))
Thread2.start()
finally:
thread = threading.Thread(targer=server, args=settings)
thread.start()
def forward(source, destination):
string = ' '
while string:
string = source.recv(1024)
if string:
destination.sendall(string)
else:
source.shutdown(socket.SHUT_RD)
destination.shutdown(socket.SHUT_WR)
if __name__ == '__main__':
main('port-forward.config', 'error.log')
Python 3版本似乎可以正常工作,但我不确定它是否编写正确。我不清楚代码中的线程部分。Python 2版本使用线程模块,而Python 3版本使用线程模块。看看Python 2版本,它在主函数中使用锁。我需要在Python 3版本中使用锁吗?另一个问题是,在Python 3代码的服务器函数中,我应该加入两个线程(线程1和线程2)吗?
无论Python版本如何,我还有一个问题是关于服务器函数的参数。我理解“settings”变量是一个列表。为什么在“settings”参数之前需要有一个星号?我确实查看了Python文档中的以下页面 https://docs.python.org/3/tutorial/controlflow.html#arbitrary-argument-lists,但我不明白为什么没有星号传递设置列表就不能正常工作。
我还有一个问题是关于Python 2代码中的线程锁。为什么锁被获取两次?我尝试删除一个锁,程序立即在启动后结束。