私の単純な socket serverの場合、呼び出される関数が必要serverloop
です (関数のスキルを向上させようとしています)。この関数では、ループは常に潜在的なクライアントとの接続を試みます。
def serverloop(s):
while True:
conn, addr = s.accept()
print "Connected with", addr
ただし、conn
他の機能を実行するには必要です。
def send_msg(conn):
#Send some data to the remote server
my_message = raw_input(">>>")
#set the whole string
conn.sendall(my_message)
という行で conn を強制的にグローバルにしようとしましたがglobal conn
、それでもエラーが発生しました。
NameError: global name 'conn' is not defined
注:スレッドを使用する必要があります。
関数内で定義されているにもかかわらず、conn 変数を呼び出すにはどうすればよいですか?