gpt4 book ai didi

Python Socket,我如何在 s.send 和 conn.send 之间进行选择?

转载 作者:行者123 更新时间:2023-11-30 22:11:43 27 4
gpt4 key购买 nike

def send_Button():
try:
myMsg = "ME: " + text.get()
msg = text.get()
conn.send(msg) ###
textBox.insert(END, myMsg + "\n")
textEntry.delete(0, END)
textBox.yview_pickplace("end")
except NameError:
myMsg = "ME: " + text.get()
msg = text.get()
conn.send(msg) ###
textBox.insert(END, myMsg + "\n")
textEntry.delete(0, END)
textBox.yview_pickplace("end")

该程序使用python2.7中带有socket的tkinter模块。我的程序允许您连接到服务器以与您聊天或托管服务器以供其他人连接到您,但每当我尝试测试它时,带有“###”的行总是会出现错误,并且它不起作用,出现的错误是:“NameError:全局名称'conn'未定义”或“错误:[Errno 10057]发送或接收数据的请求被禁止,因为套接字未连接并且(当使用 sendto 调用在数据报套接字上发送时)未提供地址”。

请问有什么帮助吗?

最佳答案

我认为您正在尝试让程序充当客户端或服务器,只是将 s.send() 更改为 conn.send() 遗憾的是事情没那么简单。

套接字初始化

在发送或接收数据之前必须初始化套接字。

对于客户来说通常是这样的。

send_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) # Create the socket
send_socket.connect((serverIp, serverPort)) # Connect to the server
send_socket.send(data) # Send the data to the server

对于服务器来说就像这样:

listen_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) # Create the socket
listen_socket.bind(("0.0.0.0", port)) # Set the socket to accept client from every interface on the port port
listen_socket.listen(1) # Put the server on listen on the port setted before
accept_socket, addr = self.listen_socket.accept() # when a client connect return the socket to talk with it
data = self.accept_socket.recv(buffer_size) # Receive data form the client of max size buffer_size

文档示例

从你的问题来看,我猜你正在谈论 s.send()conn.send()

this example from the python 2.7 socket docs

Here are four minimal example programs using the TCP/IP protocol: a server that echoes all data that it receives back (servicing only one client), and a client using it. Note that a server must perform the sequence socket(), bind(), listen(), accept() (possibly repeating the accept() to service more than one client), while a client only needs the sequence socket(), connect(). Also note that the server does not sendall()/recv() on the socket it is listening on but on the new socket returned by accept().

客户端

Echo client program

import socket
HOST = 'daring.cwi.nl' # The remote host
PORT = 50007 # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall('Hello, world')
data = s.recv(1024)
s.close()
print 'Received', repr(data)

客户端非常简单,它创建套接字s,然后在使用s.connect()后,它只是通过它发送数据。

服务器

服务器端有 sconn

Echo server program

import socket
HOST = '' # Symbolic name meaning all available interfaces
PORT = 50007 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
data = conn.recv(1024)
if not data:
break
conn.sendall(data)
conn.close()

首先,我们创建一个套接字 s,服务器将在其上监听,然后使用 s.accept() 它将等待客户端连接到服务器然后返回conn,它是连接的客户端的套接字。

因此,要向客户端接收或发送数据,您必须使用 conn

注释

正如这两个示例中的文档所述,服务器仅接受一个客户端。因此,如果您想处理多个客户端,则必须重复 accept 步骤,并可能为每个客户端生成一个新线程,以便其他客户端不必互相等待。

关于Python Socket,我如何在 s.send 和 conn.send 之间进行选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51331426/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com