本文整理汇总了Python中socket.socket.send方法的典型用法代码示例。如果您正苦于以下问题:Python socket.send方法的具体用法?Python socket.send怎么用?Python socket.send使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类socket.socket
的用法示例。
在下文中一共展示了socket.send方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: prepare_socket_for_tls_handshake
# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import send [as 别名]
def prepare_socket_for_tls_handshake(self, sock: socket.socket) -> None:
# Grab the banner
if self.SHOULD_WAIT_FOR_SERVER_BANNER:
sock.recv(2048)
# Send Start TLS
sock.send(self.START_TLS_CMD)
if self.START_TLS_OK not in sock.recv(2048):
raise StartTlsError(self.ERR_NO_STARTTLS)
示例2: handlerequest
# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import send [as 别名]
def handlerequest(clientsocket: socket.socket):
conn = connecttodb()
cursor = conn.cursor()
tagid = bytes.decode(clientsocket.recv(3))
query = 'select * from tagdata where id={0}'.format(tagid)
cursor.execute(query)
s=[]
for (id, location, officehours, otherinfo, description) in cursor:
s = [str(id)+','+location + ',', officehours + ',', otherinfo + ',', description]
s.insert(0, str(len(''.join(s)))+',')
clientsocket.send(bytes(''.join(s),encoding='utf8'))
clientsocket.close()
示例3: call
# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import send [as 别名]
def call(self, *args, **kwargs):
"""
Remote call.
"""
socket = Socket(AF_INET, SOCK_STREAM)
socket.connect(self.address)
try:
method = Document()
method.name = self.name
method.args = args
method.kwargs = kwargs
socket.send(method.dump())
reply = socket.recv(4096)
result = Document()
result.load(reply)
return result
finally:
socket.close()
示例4: _initialize_telnet
# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import send [as 别名]
def _initialize_telnet(connection: socket.socket) -> None:
logger.info('Initializing telnet connection')
# Iac Do Linemode
connection.send(IAC + DO + LINEMODE)
# Suppress Go Ahead. (This seems important for Putty to do correct echoing.)
# This will allow bi-directional operation.
connection.send(IAC + WILL + SUPPRESS_GO_AHEAD)
# Iac sb
connection.send(IAC + SB + LINEMODE + MODE + int2byte(0) + IAC + SE)
# IAC Will Echo
connection.send(IAC + WILL + ECHO)
# Negotiate window size
connection.send(IAC + DO + NAWS)
示例5: get_page
# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import send [as 别名]
def get_page(server_address, request_string):
try:
client_socket = Socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((server_address, REMOTE_PORT))
client_socket.send(request_string.encode())
reply = bytes()
while True:
part_body = client_socket.recv(BUFFER_SIZE).decode("utf-8", "ignore")
# print part_body
if not len(part_body):
break
reply += part_body
finally:
client_socket.close()
return reply
示例6: do_handshake
# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import send [as 别名]
def do_handshake(client: socket):
"""Get a client socket and do the handshake of it.
This method receives a client socket that simulates a switch on the
network and does the OpenFlow handshake process with a running controller
on the network.
Args:
client (socket): a socket object connected to the controller.
Returns:
The client with the handshake process done.
"""
# -- STEP 1: Send Hello message
client.send(Hello(xid=3).pack())
# -- STEP 2: Wait for Hello response
binary_packet = b''
while len(binary_packet) < 8:
binary_packet = client.recv(8)
header = Header()
header.unpack(binary_packet)
# -- STEP 3: Wait for features_request message
binary_packet = b''
# len() < 8 here because we just expect a Hello as response
while len(binary_packet) < 8:
binary_packet = client.recv(8)
header = Header()
header.unpack(binary_packet)
# -- STEP 4: Send features_reply to the controller
basedir = os.path.dirname(os.path.abspath(__file__))
raw_dir = os.path.join(basedir, 'raw')
message = None
with open(os.path.join(raw_dir, 'features_reply.cap'), 'rb') as file:
message = file.read()
client.send(message)
return client
示例7: _InProgressSocketManager
# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import send [as 别名]
class _InProgressSocketManager(object):
"""
This class does not actually work right now. It's something that I'm
writing that will some day replace InputThread and OutputThread and allow
Autobus to function using only a single thread for each Bus instance. So
it's a work in progress, and it doesn't actually work right now.
"""
def __init__(self):
# Set up the interrupt socket
interrupt_server = Socket()
interrupt_server.bind(("localhost", 0))
interrupt_server.listen(1)
self.interrupt_writer = Socket()
self.interrupt_writer.setblocking(False)
self.interrupt_writer.connect("localhost", interrupt_server.getsockname()[1])
self.interrupt_reader = interrupt_server.accept()
interrupt_server.shutdown(SHUT_RDWR)
interrupt_server.close()
self.interrupt_reader.setblocking(False)
self.interrupt_writer.setblocking(False)
def loop(self):
pass
def run_sync(self, function, timeout=None):
q = Queue()
@self.run_async
def new_function():
q.put(function())
try:
return q.get(timeout=timeout)
except Empty:
raise exceptions.TimeoutException
def run_async(self, function):
pass
def interrupt(self):
self.interrupt_writer.send(0)
示例8: mysend_fully
# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import send [as 别名]
def mysend_fully(sock: socket.socket, msg: bytes):
totalsent = 0
msglen = len(msg)
while totalsent < msglen:
# wait until ready to send (10 seconds)
rrcv, rsnd, rerr = select.select([], [sock], [], 10)
if sock not in rsnd: # still not ready to send!
return False
# send what we can...
nsent = sock.send(msg[totalsent:])
if nsent == 0:
# raise RuntimeError("socket connection broken")
return False
totalsent = totalsent + nsent
return True
示例9: return_data
# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import send [as 别名]
def return_data(client: socket.socket):
client.send('{"data": [4, 8, 9, 3, 7, 5, 2, 6]}'.encode())
示例10: return_icon
# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import send [as 别名]
def return_icon(client: socket.socket):
client.send("/r/n/r/n".encode())
示例11: return_d3
# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import send [as 别名]
def return_d3(client: socket.socket):
with open("d3/d3.js") as file:
status = "HTTP/1.1 200 OK"
t = "\r\n\r\n".join([status, file.read()])
client.send(t.encode())
示例12: return_html
# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import send [as 别名]
def return_html(client: socket.socket):
with open("template.html") as file:
status = "HTTP/1.1 200 OK"
t = "\r\n\r\n".join([status, file.read()])
client.send(t.encode())
示例13: client_sender
# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import send [as 别名]
def client_sender(nickname, client_socket: socket.socket,
message_queue: queue.Queue,
delete_observer_queue_queue):
messageToSend = '\n>>> ** ' + nickname + ' has entered **' + "\n"
write(messageToSend, client_socket, message_queue)
client_socket.send('\n>>> Type /cmds to see the possible commands\n'.encode()) #just tells the user how to use the server
while True:
message_bytes = client_socket.recv(1024)
try:
message = message_bytes.decode().rstrip()
except UnicodeDecodeError:
messageToSend = '\n>> ** ' + nickname + ' has quit **' + "\n"
write(messageToSend, client_socket, message_queue)
index = userlist.index(nickname)
userlist.remove(nickname)
del socketlist[index]
return
if len(message) == 0:
messageToSend = '\n>>> ** ' + nickname + ' has quit **' + "\n" #If you send a message of length 0, the server kicks you
write(messageToSend, client_socket, message_queue)
try:
index = userlist.index(nickname)
userlist.remove(nickname)
del socketlist[index]
except ValueError:
p = 1
kicked = 1
return
elif message.startswith('/nick '):
new_nickname = message.replace('/nick ', '')
if " " in new_nickname:
client_socket.send("\n>>> No Spaces Allowed in Your Name!\n".encode())
else: #Lets users change their nickname. No spaces allowed.
messageToSend = '\n>>> ** ' + nickname + ' is now known as ' + new_nickname + ' **' + "\n"
write(messageToSend, client_socket, message_queue)
index = userlist.index(nickname)
userlist.remove(nickname)
nickname = new_nickname
userlist.insert(index, nickname)
elif message.startswith('/ulist'):
ulist = '\n'.join(userlist)
messageToSend = "\n>>> User List: \n"
client_socket.send(messageToSend.encode()) #shows the user that sent the command the list of users currently in the server
client_socket.send(ulist.encode())
enter = "\n"
client_socket.send(enter.encode())
elif message.startswith('/votekick '):
kickname = message.replace("/votekick ", '')
try:
userlist.index(kickname)
messageToSend = "\n>>> " + nickname + " wants to kick " + message[10:] + "!" + "\n" #initiates a vote to kick a specific person in the server.
write(messageToSend, client_socket, message_queue)
initializeVote()
index2 = userlist.index(message[10:])
global votekicked
votekicked = index2
except ValueError:
messageToSend = "\n>>> " + kickname + " was not found." + "\n" #the person has to actually exist in the server
write(messageToSend, message_queue, message_queue)
elif message.startswith("/kick "):
kickname = message.replace("/kick ", '')
index1 = userlist.index(kickname)
kick_socket = socketlist[index1]
del userlist[index1]
kick_socket.shutdown(socket.SHUT_RDWR) #hidden command, used to kick people whenever you want
kick_socket.close()
if kick_socket == client_socket:
return
messageToSend = "\n>>> " + userlist[index1] + " was kicked" + "\n"
write(messageToSend, client_socket, message_queue)
elif message.startswith('/agree'):
messageToSend = nickname + " agrees!" + "\n"
write(messageToSend, client_socket, message_queue)
index = socketlist.index(client_socket)
votelist[index] = 1 #tied to the votekick option, allows people to vote on if they want the person kicked
votes = 0
global votekicked
for x in votelist:
if votelist[x] == 1:
votes = votes + 1
if votes>(len(userlist)/2): #requires 1/2 of the server to agree
kick_socket = socketlist[votekicked]
kick_socket.shutdown(socket.SHUT_RDWR)
kick_socket.close()
#client_socket.shutdown(socket.SHUT_RDWR)
#client_socket.close()
messageToSend = "\n>>> " + userlist[votekicked] + " was kicked" + "\n"
write(messageToSend, client_socket, message_queue)
#.........这里部分代码省略.........
示例14: socket_send
# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import send [as 别名]
def socket_send(sock: socket.socket, data: str) -> int:
# return socket.send(data) # Python 2
return sock.send(data.encode("ascii")) # Python 3