0
我的代碼出現問題。
連接應該可以工作,但服務器不會得到任何東西,即使我正在發送數據。Socket無法在Python中連接
你能查看我的代碼並幫助我嗎?
import socket
def inviare(ip,port):
file_name = raw_input("File name? ")
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.connect((ip,port))
file_open = open(file_name,"r")
file_content = file_open.read()
print file_content
sock.send(file_content)
file_open.close()
sock.close()
def ricevere(ip,port):
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.bind((ip,port))
sock.listen(5)
while 1:
(connection, adress) = sock.accept()
try:
file_data = sock.recv(6000)
filewrite = open("Down.txt","w")
print file_data.read()
filewrite.write(file_data.readlines())
filewrite.close
except:
pass
def main():
command = raw_input("Send or receive? ");
if(command == "receive"):
ip = raw_input("Ip ")
port = input("Port ")
ricevere(ip,port)
elif(command == "send"):
ip = raw_input("Ip ?")
port = input("Port?")
inviare(ip,port)
if __name__ == '__main__':
main()
我試圖在多臺機器上運行它,並改變了很多東西,但什麼都沒有發生。同樣的問題,但它不輸出任何錯誤!
也許你想在except-子句中更換'pass',以確保在try-block內沒有什麼不好的事情發生。 (由於'close'後缺少括號,你也不關閉文件)。 – Hyperboreus