Python 在套接字编程中,conn和通道之间有什么区别
我目前正在编写我的第一个基本套接字服务器,我偶然发现了服务器与客户端通信的两种不同形式——线程(我认为在python中使用Python 在套接字编程中,conn和通道之间有什么区别,python,sockets,Python,Sockets,我目前正在编写我的第一个基本套接字服务器,我偶然发现了服务器与客户端通信的两种不同形式——线程(我认为在python中使用conn)和通道,我不太理解,但它们似乎是客户端和服务器之间通信的另一种方式。两者之间的区别是什么?如何最好地使用它们 这是要求的代码-请原谅我的错误:) 连接/线程 def clientthread(conn): #Sending message to connected client #This only takes strings (words)
conn
)和通道,我不太理解,但它们似乎是客户端和服务器之间通信的另一种方式。两者之间的区别是什么?如何最好地使用它们
这是要求的代码-请原谅我的错误:)
连接/线程
def clientthread(conn):
#Sending message to connected client
#This only takes strings (words)
conn.send("Welcome to the server. Type something and hit enter\n")
#loop so that function does not terminate and the thread does not end
while True:
#Receiving from client
data = conn.recv(1024)
if not data:
break
conn.sendall(data)
print data
#To close the connection
conn.close()
while True:
#Wait to accept a connection - blocking call
conn, addr = s.accept()
#display client information (IP address)
print 'Connected with ' + addr[0] + ':' + str(addr[1])
#Start new thread takees 1st argument as a function name to be run, second
#is the tuple of arguments to the function
start_new_thread(clientthread ,(conn,))
while True:
#Wait to accept a connection - blocking call
channel, details = s.accept()
#display client information (IP address)
print 'Connected with ', details
#Start new thread takees 1st argument as a function name to be run, second
#is the tuple of arguments to the function
#Sending message to connected client
#This only takes strings (words)
intro = "Welcome to the server. Type something and hit enter"
print channel.recv(len(intro))
channel.send(intro)
#loop so that function does not terminate and the thread does not end
while True:
#Receiving from client
data = channel.recv(1024)
if not data:
break
channel.sendall(data)
print data
频道
def clientthread(conn):
#Sending message to connected client
#This only takes strings (words)
conn.send("Welcome to the server. Type something and hit enter\n")
#loop so that function does not terminate and the thread does not end
while True:
#Receiving from client
data = conn.recv(1024)
if not data:
break
conn.sendall(data)
print data
#To close the connection
conn.close()
while True:
#Wait to accept a connection - blocking call
conn, addr = s.accept()
#display client information (IP address)
print 'Connected with ' + addr[0] + ':' + str(addr[1])
#Start new thread takees 1st argument as a function name to be run, second
#is the tuple of arguments to the function
start_new_thread(clientthread ,(conn,))
while True:
#Wait to accept a connection - blocking call
channel, details = s.accept()
#display client information (IP address)
print 'Connected with ', details
#Start new thread takees 1st argument as a function name to be run, second
#is the tuple of arguments to the function
#Sending message to connected client
#This only takes strings (words)
intro = "Welcome to the server. Type something and hit enter"
print channel.recv(len(intro))
channel.send(intro)
#loop so that function does not terminate and the thread does not end
while True:
#Receiving from client
data = channel.recv(1024)
if not data:
break
channel.sendall(data)
print data
他们做的事情完全一样,除了一个使用频道
,另一个使用conn
,我不知道你在说什么。您可以展开、显示一些示例代码或链接到某些内容吗?我怀疑您正在考虑不同的并发方案(异步、分叉、线程化、队列、greenlet等),这是一个独立于套接字的考虑因素,在客户端和服务器上不必相同。套接字就是套接字。conn
和channels
在Python中没有特殊意义-但如果有用的话,conn=socket()
有时会出现在代码中。@thomas so。。。它们只是常规变量,因此可以互换?Thanks@xxmbabanexx对-我们都想知道你在说什么,因为你刚才问我们foo
和bar
有什么不同。你需要告诉我们控制室和频道是什么,我们不知道。但这还不够——您需要显示一些代码并编写一个更具体的问题。听起来你还没有语言来描述这些。如果你不确定下一步该做什么,学习一些语言或构建一个(或每个语言中的一个),并询问有关代码的问题。顺便说一句,不要使用线程。尝试以异步方式写入服务器。这是最好的模型,您应该学习如何异步编写程序。只需在socket()上设置非阻塞。