当前位置: 动力学知识库 > 问答 > 编程问答 >

Python 3.4 Sockets sendall function

问题描述:

import socket

def functions():

print ("hello")

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

server_address = ('192.168.137.1', 20000)

sock.bind(server_address)

sock.listen(1)

conn, addr = sock.accept()

print ('Connected by', addr)

sock.listen(1)

conn.sendall(b"Welcome to the server")

My question is how to send a function to the client,

I know that conn.sendall(b"Welcome to the server") will data to the client.

Which can be decoded.

I would like to know how to send a function to a client like

conn.sendall(function()) - this does not work

Also I would like to know the function that would allow the client to receive the function I am sending

I have looked on the python website for a function that could do this but I have not found one.

网友答案:

The functionality requested by you is principally impossible unless explicitly coded on client side. If this were possible, one could write a virus which easily spreads into any remote machine. Instead, this is client right responsibility to decode incoming data in any manner.

Considering a case client really wants to receive a code to execute, the issue is that code shall be represented in a form which, at the same time,

  • is detached from server context and its specifics, and can be serialized and executed at any place
  • allows secure execution in a kind of sandbox, because a very rare client will allow arbitrary server code to do anything at the client side.

The latter is extremely complex topic; you can read any WWW browser security history - most of closed vulnerabilities are of issues in such sandboxing.

(There are environments when such execution is allowed and desired; e.g. Erlang cookie-based peering cluster. But, in such cluster, side B is also allowed to execute anything at side A.)

You should start with searching an execution environment (high-level virtual machine) which conforms to your needs in functionality and security. For Python, you'd look at multiprocessing module: its implementation of worker pools doesn't pass the code itself, but simplifies passing data for execution requests. Also, passing of arbitrary Python data without functions is covered with marshal and pickle modules.

分享给朋友:
您可能感兴趣的文章:
随机阅读: