python - 无法让安装了ubuntu的电脑充当简单的fie共享程序中的服务器

标签 python linux sockets ubuntu

服务器代码:

import socket
import os.path
host=''
port=9988
sock=socket.socket()
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((host,port))
sock.listen(5)
while True:
    new_socket,(h,p)=sock.accept()
    print "Client connected with ip address : "+h
    f=new_socket.recv(1024)
    print h+" asking for "+f
    if os.path.isfile(f) and os.access(f, os.R_OK):
            temp = open(f,'rb')
            size=os.path.getsize(f)
            var='y '+str(size)
            new_socket.send(var)
            ch=new_socket.recv(1024)
            if ch=='y':
                new_socket.send(temp.read())
            print f+" sent successfully to "+h
            temp.close()
            new_socket.close()
    else:
            new_socket.send("n")
            new_socket.close()
sock.close()

客户端代码:

import socket
host="ip address of computer as server"
port=9988
while True:
    soc=socket.socket()
    soc.connect((host,port))
    f=raw_input("Enter the name of the file you wish to download : ")
    soc.send(f)
    msg=soc.recv(1024)
    if msg[0]=='y':
        print "The file exists, total size : "+msg[2:]
        ch=raw_input("Download ? (y/n) : ")
        if ch=='y'or ch=='Y':
            fo=open("new_"+f,'wb')
            soc.send('y')
            rcv=soc.recv(1024)
            fo.write(rcv)
            rcv=soc.recv(1024)
            while rcv:
                fo.write(rcv)
                rcv=soc.recv(1024)
            print "File download complete. "
            fo.close()
    else:
        print "No file with this name found on the server. "
    ch=raw_input("Want to continue? (y/n) : ")
    if ch=='n'or ch=='N':
        break
    soc.close()

当我在 ubuntu 笔记本电脑上运行服务器代码并在 Windows 笔记本电脑上运行客户端代码时,我收到错误消息:错误:[Errno 10060] 连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机未能响应

如果我在 Windows 上运行服务器并在 ubuntu 上运行客户端,文件共享工作得很好。如何让装有 ubuntu 的计算机充当服务器?

它可以在本地主机上运行。

最佳答案

首先,尝试禁用ufw和selinux,然后重试。使用 netstat 命令查看服务器上打开了哪些端口并处于“监听”状态,此外,您还可以使用 tcpdump 查看网络上发生的情况。

关于python - 无法让安装了ubuntu的电脑充当简单的fie共享程序中的服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28871547/

相关文章:

python - rs-convert 不从 rosbag 文件生成 .ply 文件

python - 文本文件中的整数值未更改

c - sig_atomic_t 实际上是如何工作的?

linux - 如何从 CentOS 中删除 golang 包

android - Socket EADDRINUSE(地址已被使用)

python - Mpmath.meijerg : Order of arguments

python - Pandas :将列的值分配到字典值设置的限制

php - Openshift 使用替代容器镜像

php - 如何创建雅虎 smtp 服务器的套接字?

sockets - POSIX POLLOUT和阻止文件描述符