本文整理匯總了Python中transport.Transport.receive方法的典型用法代碼示例。如果您正苦於以下問題:Python Transport.receive方法的具體用法?Python Transport.receive怎麽用?Python Transport.receive使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類transport.Transport
的用法示例。
在下文中一共展示了Transport.receive方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: Player
# 需要導入模塊: from transport import Transport [as 別名]
# 或者: from transport.Transport import receive [as 別名]
class Player(object):
auth_timeout = 5
def __init__(self, socket, address):
self.transport = Transport(socket, address)
self.host = address[0]
self.port = address[1]
self.health = 100
def close(self):
self.transport.send(Message.exit().pack())
self.transport.close()
def auth(self):
recv = None
with Timeout(Player.auth_timeout, PlayerError("Authentication timed out")):
try:
msg = self.transport.receive()
except PlayerError as e:
print("error on receive")
print(e)
return False
try:
msg = Message.parse(msg)
except MessageError:
print("bad message")
return False
if msg.type == "nick":
self.name = msg.payload
self.transport.send(Message.auth_success().pack())
return True
return False
def listen(self):
return self.transport.receive()
def send(self, msg):
self.transport.send(msg)
示例2: Client
# 需要導入模塊: from transport import Transport [as 別名]
# 或者: from transport.Transport import receive [as 別名]
class Client(Greenlet):
def __init__(self, host, port, nick):
Greenlet.__init__(self)
self.host = host
self.port = port
self.name = nick
def _run(self):
self.connect()
if not self.auth():
self.exit("Could not connect to server")
print("Auth successful")
self.opponent = self.opponent_wait()
self.start_wait()
self.play()
def wait(self, msg_type):
"""
Waits for a specific message type, then returns the received message
"""
while True:
rec = self.transport.receive()
try:
msg = Message.parse(rec)
except MessageError:
continue
if msg.type == msg_type:
return msg
def opponent_wait(self):
msg = self.wait('opponent')
return msg.payload
def start_wait(self):
while True:
msg = self.wait('game')
if msg.payload == 'start':
break
def play(self):
sensor = gevent.spawn(self.handle_punch)
sensor.join()
def handle_punch(self):
while True:
gevent.sleep(7)
#print "sending punch"
self.transport.send(Message.punch().pack())
def exit(self, msg):
print(msg)
raise GreenletExit
def connect(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((self.host, self.port))
print dir(sock)
self.transport = Transport(sock, (self.host, self.port))
def auth(self):
self.transport.send(Message.nick(self.name).pack())
data = self.transport.receive()
try:
msg = Message.parse(data)
except MessageError:
return False
if msg.type == 'auth' and msg.payload == 'success':
return True
else:
return False
示例3: Client
# 需要導入模塊: from transport import Transport [as 別名]
# 或者: from transport.Transport import receive [as 別名]
class Client(Greenlet):
def __init__(self, host, port, nick):
Greenlet.__init__(self)
self.host = host
self.port = port
self.name = nick
def _run(self):
self.connect()
if not self.auth():
self.exit("Could not connect to server")
print("Auth successful")
self.opponent = self.opponent_wait()
self.start_wait()
self.play()
def wait(self, msg_type):
"""
Waits for a specific message type, then returns the received message
"""
while True:
rec = self.transport.receive()
try:
msg = Message.parse(rec)
except MessageError:
continue
if msg.type == msg_type:
return msg
def opponent_wait(self):
msg = self.wait('opponent')
return msg.payload
def start_wait(self):
while True:
msg = self.wait('game')
if msg.payload == 'start':
break
def play(self):
sensor = gevent.spawn(self.handle_punch)
sensor.join()
def handle_punch(self):
while True:
gevent.sleep(2)
""" Record a few seconds of audio and save to a WAVE file. """
from scipy.io.wavfile import read, write
from scipy.fftpack import rfft, irfft
from scipy import *
from pylab import *
import numpy as np
import pyaudio
import matplotlib.pyplot as plt
import wave
import sys
last = 0.
last2 = 0.
cal = []
mean = 0.
std = 0.
hit = 0
chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
#RECORD_SECONDS = 0.1
WAVE_FILENAME = '/tmp/ashfs/output.wav'
p = pyaudio.PyAudio()
stream = p.open(format = FORMAT,
channels = CHANNELS,
rate = RATE,
input = True,
output = True,
frames_per_buffer = chunk)
init = True
while True:
#print "* recording"
all = []
#for i in range(0, RATE / chunk * RECORD_SECONDS):
i = 0
while i < 5: # ~120ms
try:
data = stream.read(chunk)
all.append(data)
i = i + 1
#.........這裏部分代碼省略.........