简体   繁体   English

通过 python 套接字发送 json.dumps()

[英]sending json.dumps() via python sockets

What I'm trying to create are a set of server and client scripts;我试图创建的是一组服务器和客户端脚本; the server script prompts a user for raw input, stores that input in a dictionary and converts it to json with the json.dumps() function.服务器脚本提示用户输入原始输入,将该输入存储在字典中并使用json.dumps()函数将其转换为 json。 The converted dictionary is then stored in the jasonFile variable which is then sent to the client.然后将转换后的字典存储在jasonFile变量中,然后将其发送到客户端。 The json dictionary is working but I'm struggling with the networking side of things. json 字典正在工作,但我正在努力解决网络方面的问题。

Here is my server code:这是我的服务器代码:

def Main():
host = '0.0.0.0'
port = 5000
s.bind((host, port))
s.listen(5)

print "Server Started"


while True:
    addr = s.accept()
    print "Client Connected from IP: " + str(addr)
    serverMessage = "Connection Established: Would you like to download the Json dictionary?"
    s.send(serverMessage)
    clientReply = s.recv(1024)
    if clientReply in ['Y', 'y', 'Yes', 'yes', 'YES']:
        s.send(jasonFile)
        s.close()
    else:
        print "Connection from " + addr + " closed!"
        s.send("Connection Error!")
        s.close()

And here is my client code:这是我的客户端代码:

def Main():
    host = raw_input("Please enter the server IP you wish to connect to: ")
    port = 5000

    #define client to use socket module to connect via IPV4 and TCP only
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.connect((host, port))

    serverMessage = client.recv(1024)
    print serverMessage

    clientReply = raw_input("Type 'Yes' To download dictionary")
    if clientReply in ['Y', 'Yes', 'y', 'yes', 'YES']:
            client.send(clientReply)
            jasonRecv = client.recv(1024)
            print jasonRecv
    else:
            client.close()
            print "Disconnected from server!"

I haven't gotten as far as converting the json data back to a string on the client yet because the server throws me an error when the client tries to connect.我还没有在客户端上将 json 数据转换回字符串,因为当客户端尝试连接时服务器向我抛出一个错误。

The error message I get from IDLE is:我从 IDLE 收到的错误消息是:

Server Started
Client Connected from IP: (<socket._socketobject object at 0x000000000401E048>, ('127.0.0.1', 34375))

Traceback (most recent call last): File "D:/Server.py", line 105, in <module>
Main()

File "D:/Server.py", line 94, in Main
s.send(serverMessage)

error: [Errno 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied

I thought I was defining the address to send data to in the addr variable, but apparently not?我以为我是在addr变量中定义要发送数据的addr ,但显然不是?

Try:尝试:

conn, addr = s.accept()
...
conn.send(serverMessage)

ie replace s.即替换s. calls with conn.conn.调用conn. which represents the accepted socket connection from the client.它表示来自客户端的接受的套接字连接。

From the Python socket API :Python 套接字 API

socket.accept() socket.accept()

Accept a connection.接受连接。 The socket must be bound to an address and listening for connections.套接字必须绑定到一个地址并侦听连接。 The return value is a pair (conn, address) where conn is a new socket object usable to send and receive data on the connection, and address is the address bound to the socket on the other end of the connection.返回值是一对 (conn, address),其中 conn 是可用于在连接上发送和接收数据的新套接字对象,address 是绑定到连接另一端的套接字的地址。

Examples are provided at the end of the page.页面末尾提供了示例。

Also see the Python Socket Programming Howto另请参阅Python Socket Programming Howto

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM