def run():
    channel = grpc.insecure_channel('localhost:50051')
    try:
        grpc.channel_ready_future(channel).result(timeout=10)
    except grpc.FutureTimeoutError:
        sys.exit('Error connecting to server')
    else:
        stub = users_service.UsersStub(channel)

        metadata = [('ip', '127.0.0.1')]
        response = stub.CreateUser(
            users_messages.CreateUserRequest(username='******'),
            metadata=metadata,
        )
        if response:
            print("User created:", response.user.username)

        request = users_messages.GetUsersRequest(user=[
            users_messages.User(username="******", user_id=1),
            users_messages.User(username="******", user_id=1),
            users_messages.User(username="******", user_id=1),
        ])
        response = stub.GetUsers(request)
        for resp in response:
            print(resp)
示例#2
0
def run():
    channel = grpc.insecure_channel(
        'localhost:50051')  #create a channel to our server
    try:
        grpc.channel_ready_future(channel).result(
            timeout=10)  #client waits for timeout
    except grpc.FutureTimeoutError:
        sys.exit('Error connecting to server')
    else:
        stub = users_service.UsersStub(
            channel
        )  #else create a BertSqquadStub object and pass channel as arg
        metadata = [('ip', '127.0.0.1')]
        response = stub.CreateUser(
            users_messages.CreateUserRequest(username='******'),
            metadata=metadata,
        )
        if response:
            print("User created:", response.user.username)
        request = users_messages.GetUsersRequest(  #request is of repeated type, so it is a list
            user=[
                users_messages.User(
                    username="******",
                    user_id=1),  #repeated User in GetUsersRequest message
                users_messages.User(username="******", user_id=1)
            ])
        response = stub.GetUsers(
            request)  #result of GetUsers is a stream, so iterate over it
        for resp in response:
            print(resp)
示例#3
0
def run():
    ip_address = "localhost"
    if (len(sys.argv) > 1):
        ip_address = sys.argv[1]
    with grpc.insecure_channel(ip_address + ':10001') as channel:
        stub = users_pb2_grpc.UsersStub(channel)
        menuSelect(stub)
        quit()
示例#4
0
    def __init__(self):
        self.host = 'localhost'
        self.port = 50051

        self.channel = grpc.insecure_channel('{}:{}'.format(
            self.host, self.port))

        self.stub = service.UsersStub(self.channel)
示例#5
0
def test_get_pets():
    with grpc.insecure_channel(os.environ['USERS_SERVICE']) as channel:
        stub = u_grpc.UsersStub(channel)
        response = stub.GetUser(u.UserRequest(user_id=1))
        print(response)

        response = stub.GetUserPets(u.UserRequest(user_id=1))
        print(response)
示例#6
0
    def Connect_to_server(self):
        channel = grpc.insecure_channel('{}:{}'.format(self.ip, str(self.port)))
        # Try connecting to server for 10 seconds
        try:
            grpc.channel_ready_future(channel).result(timeout=10)
        except grpc.FutureTimeoutError:
            sys.exit("[SPARTAN] Error connecting to server.")

        self.stub = users_pb2_grpc.UsersStub(channel)
示例#7
0
async def run_grpc():
    channel = grpc.insecure_channel('[::]:50051')
    stub = users_pb2_grpc.UsersStub(channel)

    request = users_pb2.UsersRequest()
    size = 0
    for _ in range(REQUESTS):
        response = stub.get(request)
        size += response.ByteSize()

    return size
示例#8
0
async def grpc_stream():
    channel = grpc.insecure_channel('[::]:50051')
    stub = users_pb2_grpc.UsersStub(channel)

    request = users_pb2.UsersRequest()
    responses = stub.get_stream(request)

    size = 0
    for response in responses:
        size += response.ByteSize()

    return size
示例#9
0
def main():
    grpc_port = os.environ.get("GRPC_PORT", 50051)

    address = f"localhost:{grpc_port}"
    with grpc.insecure_channel(address) as channel:
        stub = users_pb2_grpc.UsersStub(channel)
        # Insert users.
        insert_user(stub, "Bob", "Green")
        insert_user(stub, "Alice", "Redmond")
        # Get users.
        print("Retrieving users:")
        for user in stub.GetUsers(_EMPTY):
            print(f"   User:\n{textwrap.indent(str(user), '      ')}")
示例#10
0
 def RequestComments(self, request, context):
     contentId = request.contentId
     comments = list(
         filter(lambda comment: comment['contentId'] == contentId, data))
     response = comments_pb2.CommentsResponse()
     channel = grpc.insecure_channel('users-module:22222')
     stub = users_pb2_grpc.UsersStub(channel)
     for item in comments:
         comment = response.comments.add()
         comment.content = item['comment']
         responseUser = stub.RequestUser(
             users_pb2.UserRequest(userId=item['userId']))
         comment.name = responseUser.name
         comment.imageUrl = responseUser.imageUrl
     return response
示例#11
0
文件: client.py 项目: 1UC1F3R616/gRPC
 def run(self):
     global count
     while True:
         channel = grpc.insecure_channel('localhost:50051')
         try:
             grpc.channel_ready_future(channel).result(timeout=10)
         except grpc.FutureTimeoutError:
             sys.exit('Error connecting to server')
         else:
             stub = users_service.UsersStub(channel)
             metadata = [('ip', '127.0.0.1')]
             # replace with file big file
             data = ''.join(secrets.choice(string.ascii_uppercase + string.digits)
                            for i in range(1024*1024))
             response = stub.CreateUser(
                 users_messages.CreateUserRequest(username=data),
                 metadata=metadata,
             )
             if response:
                 count += 1
                 print(str(count))