2012-03-28 2 views
1

私はこのブログの新機能ですが、ここでは多くの回答が見つかりました。 私は職場のLinuxボックスにインストールされている古いバージョンのtclで、IPv6はサポートしていません。 tclを使っていくつかのIPv6機能をテストする必要があり、IPv6ソケットを開く必要があります。 私はpythonを使ってこれをやりましたが、私の問題はtclとpythonの間を行き来しています。pythonとtclとの間でデータを送受信する

私はPython上にサーバーを実装し、そのサーバーとtcl上で通信するクライアントを実装しました。 私が直面している問題は、tclから次のことをする能力です: Pythonから読み込む - > Pythonに書き込む - > Pythonから読み込む - > Pythonに書き込む......(あなたはポイントを得る)

私はfileeventとvwaitを使用しようとしましたが、うまくいかなかった。誰もそれを前にしたことがありますか?

+0

これを確認してください:http://stackoverflow.com/questions/267420/tcl-two-way-communication-between-threads-in-windows TCLについて<-> TCL通信ですが、私はあなたがそれはTclに<-> Pythonの通信 – stanwise

+3

私は明らかに、ユーモラスな答えを追加... Pythonをプロキシとして使用し、PythonでIPv4サーバソケットを開き、Tclで接続し、Tclから取得したものをIPv6経由で送信します。 – RHSeeger

+0

Tcl 8.6b2を使用していますか?それはIPv6をサポートすべきです(私はそれがb2によって行われたと思います...) –

答えて

0

Pythonのサーバー:

import socket 
host = '' 
port = 45000 
s = socket.socket() 
s.bind((host, port)) 
s.listen(1) 
print "Listening on port %d" % port 
while 1: 
    try: 
     sock, addr = s.accept() 
     print "Connection from", sock.getpeername() 
     while 1: 
      data = sock.recv(4096) 
      # Check if still alive 
      if len(data) == 0: 
       break 
      # Ignore new lines 
      req = data.strip() 
      if len(req) == 0: 
       continue 
      # Print the request 
      print 'Received <--- %s' % req 
      # Do something with it 
      resp = "Hello TCL, this is your response: %s\n" % req.encode('hex') 
      print 'Sent  ---> %s' % resp 
      sock.sendall(resp) 
    except socket.error, ex: 
     print '%s' % ex 
     pass 
    except KeyboardInterrupt: 
     sock.close() 
     break 

TCLクライアント:サーバーの

set host "127.0.0.1" 
set port 45000 
# Connect to server 
set my_sock [socket $host $port] 
# Disable line buffering 
fconfigure $my_sock -buffering none 
set i 0 
while {1} { 
    # Send data 
    set request "Hello Python #$i" 
    puts "Sent  ---> $request" 
    puts $my_sock "$request" 
    # Wait for a response 
    gets $my_sock response 
    puts "Received <--- $response" 
    after 5000 
    incr i 
    puts "" 
} 

出力:

$ python python_server.py 
Listening on port 45000 
Connection from ('127.0.0.1', 1234) 
Received <--- Hello Python #0 
Sent  ---> Hello TCL, this is your response: 48656c6c6f20507974686f6e202330 

Received <--- Hello Python #1 
Sent  ---> Hello TCL, this is your response: 48656c6c6f20507974686f6e202331 

クライアントの出力:

$ tclsh85 tcl_client.tcl 
Sent  ---> Hello Python #0 
Received <--- Hello TCL, this is your response: 48656c6c6f20507974686f6e202330 

Sent  ---> Hello Python #1 
Received <--- Hello TCL, this is your response: 48656c6c6f20507974686f6e202331 
関連する問題