質問
pythonでサーバを立ち上げたサーバに接続を受けた時、接続元IPを得るには?
詳細
socketモジュールを使って特定のportで待つスクリプトを立ち上げます。
その時、接続してきた接続元のIPを取得したいです。
試したこと
とりあえず下記は無理でした。
python
1import socket 2 3sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 4sock.bind(("localhost", 9999)) 5sock.listen(1) 6conn, addr = sock.accept() 7print("address: %s" % sock.getpeername())
telnetした結果、python実行環境の標準出力に下記が出力
Traceback (most recent call last): File "test.py", line 8, in <module> print("address: %s" % sock.getpeername()) File "/usr/lib64/python2.7/socket.py", line 224, in meth return getattr(self._sock,name)(*args) socket.error: [Errno 107] Transport endpoint is not connected
これは自分から外部に接続する時に使うものなんですかね。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/06/26 04:16