通过网络python

时间:2015-10-17 18:06:14

标签: python

我在发送和接收数据时遇到问题。这将是一个猜数游戏最终,但目前我很难从客户端发送整数并将其存储到服务器端的变量。

我收到错误消息“TypeError:'int'不支持缓冲区接口”。我不确定这意味着什么或如何解决它。

谢谢

#Server

import socket
import math
import re
import random

goal = random.randrange(1,11)

def within(guess,goal,n):
    absValue = abs(guess - goal)
    if absValue <= n:
        return True
    else:
        return False

scoreCount = 0
guess = 0

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind(("127.0.0.1",9000))
s.listen(5)
while True:
    (c,a) = s.accept()
    print ("Received connection from", a)

    while guess!= goal:
        c.sendall('What is your guess?'.encode())
        guess =(c.recv(4096))
        print(guess)

c.close()



#Client

import socket
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(('127.0.0.1',9000))

data = s.recv(10000).decode()
print (data)


while True:

    guess = int(input("Guess: "))
    print(guess)
    s.sendall(guess)
    s.close()

0 个答案:

没有答案