简体   繁体   English

python中的套接字

[英]socket in python

i tried to do client and server and look what i do 我试图做客户端和服务器,看看我在做什么

#Server
import socket
Host=''
Port=305
OK=socket.socket()
OK.bind((Host,Port))
OK.listn(1)
OK.accept()

and another one for client 另一个给客户

#Client 
impot socket 
Host='192.168.1.4' 
Port=305
OK=socket.socket()
OK.connect((Host,Port))

First thing : for now every thing is ok but i want when client connect to server : server print "Hello Admin" in client screen 第一件事:现在一切正常,但是我希望客户端连接到服务器时:服务器在客户端屏幕上打印“ Hello Admin”

second thing : i want make like input command ! 第二件事:我想像输入命令一样! like 喜欢

COM=raw_input('enter you command system:')

then client enter dir for example then server print the result in client screen 然后客户端输入dir例如,然后服务器在客户端屏幕上打印结果

Look here, this is a simple echo server written in Python. 看这里,这是一个用Python编写的简单回显服务器。

http://ilab.cs.byu.edu/python/socket/echoserver.html http://ilab.cs.byu.edu/python/socket/echoserver.html

When you create a connection, the story isn't over. 创建连接时,故事还没有结束。 Now it's time to send data over the connection. 现在是时候通过连接发送数据了。 Create a simple "protocol" (*) and use it to transfer data from client to server and/or back. 创建一个简单的“协议”(*),并使用它来将数据从客户端传输到服务器和/或返回。 One simple example is a textual protocol of commands separated by newlines - this is similar to what HTTP does. 一个简单的示例是用换行符分隔的命令的文本协议-与HTTP相似。

(*) Protocol: an agreement between two parties on the format of their communication. (*)协议:双方之间关于其通讯格式的协议。

For Client: 对于客户:

import os

import sys

impot socket

Host=raw_input ("Please enter ip : ")

Port=raw_input ("please Enter port :")

OK=socket.socket()

OK.connect((Host,Port))

print " Enter Command")

cmd = raw_input()

os.system(cmd)

I think that your codes has an issue: you seem to have OK = socket.socket() , but I think it should be: OK = socket.socket(socket.AF_INET, socket.STREAM) , which would help if your making a connection. 我认为您的代码有问题:您似乎有OK = socket.socket() ,但我认为应该是: OK = socket.socket(socket.AF_INET, socket.STREAM) ,如果您制作一个连接。 And your server has a problem: OK.listn(1) should be OK.listen(1) . 而且您的服务器有一个问题: OK.listn(1)应该是OK.listen(1) And, don't forget about send() and recv() . 并且,不要忘记send()recv()

#Client 
import socket 
Host='192.168.1.4' 
Port=305
OK=socket.socket(socket.AF_INET, socket.STREAM)
OK.connect((Host,Port))
while True:
    com = raw_input("Enter your command: ")
    OK.send(com)
    data = OK.recv(5000) #Change the buffer if you need to, I have it setup to run 5000
    print "Received:\n" + data

which should work for the client 应该为客户工作

#Server
import socket
import os
Host=''
Port=305
OK=socket.socket(socket.AF_INET, socket.STREAM)
OK.bind((Host,Port))
OK.listen(1)
conn, addr = OK.accept()
while True:
    data = conn.recv(2048) #Change the buffer if needed
    if data == "":
        break
    r = os.system(data)
    conn.send(str(r)) #Note this will send 0 or 1, 0 = ran, 1 = error

Note: These fixes would work for Windows, I don't know about Unix systems.* 注意:这些修复程序适用于Windows,我不了解Unix系统。*

I think you might want to do something like this: 我认为您可能想要执行以下操作:

 client, addr = OK.accept()
 client.send("Hello Admin")

And then use 然后用

data = client.recv(1024)

to get data from the client. 从客户端获取数据。

If you want to get command input from the client, you just need to execute the commands the client sends and send the output back back to the client. 如果要从客户端获取命令输入,则只需执行客户端发送的命令,然后将输出发送回客户端即可。

 from commands import getoutput
 client.send(getoutput(client.recv(1024)))

Thats about the easiest solution possible. 那就是最简单的解决方案。

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

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