I have having a problem with one of my university projects. We are doing sockets and UDP at the moment. Anyway so we had to make a very simple program, server, client, password name verification.
He wanted us to make a log of things, and I created a module with methods to write to a log file, this works fine. I have called it from different places and it always works. The only time it does not work is when called from the server.
import datetime
###Appends the message to the server log with the current date and time
def AddToLogServer(message):
f = open('Log_Server', 'a')
time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
f.write(time +" " + message +"\n")
f.close()
###Appends the message to the client log with the current date and time
def AddToLogClient(message):
f = open('Log_Client', 'a')
time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
f.write(time +" " + message +"\n")
f.close()
This is the log creations. Works fine.
import socket
import sys
import Passwords
import getpass
import Log
###Create a connectionless network socket.
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
###Maximum size data.
MAX = 65535
PORT = 1060
if sys.argv[1:] == ['server']:
###Set server ip
ip = '127.0.0.1'
try:
s.bind((ip, PORT))
except:
###If the server fails to start this error message and the server should end.
print "There is an error, perhaps the ip is already being used."
s.close()
sys.exit(1)
###Add to the server log that the server has started.
Log.AddToLogServer("The server is up with the ip " + ip)
print 'Server is up and listening at', s.getsockname()
###Listen for a client to send data to the server.
while True:
data, address = s.recvfrom(MAX)
Passwords.getPasswordsAndUsers()
###Compare the name inputted to the ones in the user list
i = Passwords.findUser(data)
###Update client log
Log.AddToLogServer(address[0] + " Inputted the name " + data)
s.sendto(str(i), address)
data, address = s.recvfrom(MAX)
t = Passwords.checkPassword(data,i)
###if the password matched the user print out the correct message and send a message to the client
if t == 1:
Log.AddToLogServer(address[0] + " Inputted the correct password for that user")
print address[0] + " Has successfully entered the correct User and Password"
s.sendto("The name and password were correct", address)
###if the password did not match the user print out the correct message and send a message to the client
else:
Log.AddToLogServer(address[0] + " Inputted an incorrect password for that user")
print address[0] + " Has failed to provide the correct Password for the inputted User"
s.sendto("The password did not match the name", address)
elif sys.argv[1:] == ['client']:
###Takes in the ip and name as inputs.
serverIp = raw_input("Please enter the server ip : ");
username = raw_input("Enter your first name please: ");
### Attempts to send to the server with the given ip
try:
s.sendto(username, (serverIp, PORT))
except:
###If the send fails then an error is shown and it finishes the execution.
print "There was a problem sending to the server"
s.close()
sys.exit(1)
###Attempt to relieve data from the server, if the client does not then write the appropriate message.
try:
data, address = s.recvfrom(MAX)
except:
print "There was a problem receiving from the server"
s.close()
sys.exit(1)
data = int(data)
###If the data was -1, then the user did not exist and an error should be displayed. Otherwise ask for the
###Password and send it to the server.
if data != -1:
password = getpass.getpass("Enter your password please: ");
try:
s.sendto(password, ('127.0.0.1', PORT))
except:
print "There was a problem sending to the server"
s.close()
sys.exit(1)
else:
print "This first name was not recognised."
sys.exit(1)
###Again try to relieve data from the server and print out the output.
try:
data, address = s.recvfrom(MAX)
print data
s.close()
except:
print "There was a problem receiving to the server"
s.close()
sys.exit(1)
Client server code, the log does not work when called from the server while it is up.
I have tried reproducing the problem, but the script for the server did not execute. On my machine sys.argv[1:] returned [], so i modified this part of your script if sys.argv[1:] == ['server']: to if sys.argv[1:] == []:
Please see the response i got. You should look into that part. if sys.argv[1:] == ['server']:
The server is up with the ip 127.0.0.1
Server is up and listening at ('127.0.0.1', 1060)
Related
So I've been working on getting multiple python files to communicate, with one of those files acting as the server. The goal here is to be able to control all PCs running an instance of the "Client" script to connect to the "Server" script, from which I can control each PC.
Since I'm testing all of this on a single PC at the moment, I run an instance of the server, and the client script. Normally, I'd start up an instance of the "Server" script, than an instance of the "Client" script. When the "Client" script talks to the "Server" script for the first time, the "Server" script should show the IP that belongs to the PC that's running the "Client" script, but it isn't.
Here's my server script:
import socket,subprocess,time
print ("This is the server speaking")
def Main():
host = "0.0.0.0"
port = 2346
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host,port))
s.listen(10)
conn,addr = s.accept()
print ("connection from: " + str(addr))
while True:
message=input(" -> ")
while message !='q':
conn.send(message.encode())
data=conn.recv(16834).decode()
print ("Received data from server: " + data)
message = input (" -> ")
conn.close()
time.sleep(5)
Main()
if __name__ == "__main__":
Main()
And this is my client script:
import socket,time,os
import subprocess
def Main():
host = '127.0.0.1'
port = 2346
conn = socket.socket()
conn.connect((host,port))
while True:
#data = conn.recv(1024).decode()
data = conn.recv(16384).decode()
if not data:
break
print ("from connected user: " + str(data))
if data == "tasklist":
batcmd="tasklist"
result = subprocess.check_output(batcmd, shell=True)
data = str(result)
conn.send(data.encode())
elif "taskkill" in data:
pid = data[9:]
print (pid)
try:
batcmd="taskkill /IM %s /F" % (pid)
result = subprocess.check_output(batcmd, shell=True)
data = str(result)
conn.send(data.encode())
except:
pass
elif "ls" in data:
dirs = data[3:]
try:
x = os.listdir(dirs)
data = str(x)
conn.send(data.encode())
except:
data = "Dir. " +dirs+ " does not exist"
conn.send(data.encode())
data = str(data)
print ("sending: " + str(data))
conn.send(data.encode())
conn.close()
if __name__ == '__main__':
Main()
The "if" and "elif" statements in the middle are really just for my current commands. I'm fairly certain that the problem must lie somewhere in the server script, or the top of the client script.
The main issue here is that when I run an instance of the client script, nothing is printed on the server script's side. If anyone could help, that would be well appreciated.
Note: I'm not getting any errors.
I try to implement a server which only responds to one client at a time and client side. For the client side, I only used one connection and can send multiple requests to server. For the first request, everything goes well. For all the requests after the first one, the client will report error: socket.error: [Errno 10053] An established connection was aborted by the software in your host machine
. I post my server and client code along with the sample test below:
The server part:
import socket
import re
#create server socket. AF_INET-> ipv4. SOCK_STREAM->socket type
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
port = 20000
#bind the socket to localhost and a well-know port
serverSocket.bind((socket.gethostname(), port))
serverSocket.listen(3)
while True:
clientSocket, addr = serverSocket.accept()
print("Got a connection from %s" % str(addr))
request = clientSocket.recv(1024).decode()
print('Server received', repr(request))
splitRequest = re.split('\<|\>', request)
if splitRequest[0] == 'EXIT':
if len(splitRequest) == 1:
print "Normal exit"
elif len(splitRequest) > 1:
print splitRequest[1]
else:
if splitRequest[0] == 'GET':
fileName = splitRequest[1]
path = 'test_files/' + fileName
try :
with open(path, 'r') as serverFile:
message = serverFile.read()
print message
clientSocket.send(message)
except Exception as e:
message = str(e)
print message
clientSocket.send(message)
elif splitRequest[0] == 'BOUNCE':
message = splitRequest[1]
print message
clientSocket.sendall(message)
clientSocket.close()
The client side:
import socket
import re
def isValidRequest(input):
if re.match('GET\<.+\>', input) or re.match('BOUNCE\<.+\>', input) or input == 'EXIT' or re.match('EXIT\<.+\>', input):
return True
return False
def receiveAll(socket):
BUFF_SIZE = 4096 # 4 KiB
data = ""
while True:
part = socket.recv(BUFF_SIZE)
data += part
if len(part) < BUFF_SIZE:
# either 0 or end of data
break
return data
# create a client socket object
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# get local machine name
host = socket.gethostname()
# port number
port = 20000
# connection to hostname on the port.
s.connect((host, port))
while True:
request = raw_input()
if isValidRequest(request):
if request == 'EXIT' or re.match('EXIT\<.+\>', request):
s.send(request)
break
else:
s.send(request)
print "after send"
content = receiveAll(s)
print content
else:
print "Invalid request, please enter it again!"
# close client connection
s.close()
I run a test for request the same txt file-"MACD.txt" from server twice. The input in the console is "GET".The print message in the console for client:
*GET<MACD.txt>*
after send
MACD, short for moving average convergence/divergence, is a trading indicator used in technical analysis of stock prices, created by Gerald Appel in the late 1970s.[1] It is supposed to reveal changes in the strength, direction, momentum, and duration of a trend in a stock's price.
*GET<MACD.txt>*
socket.error: [Errno 10053] An established connection was aborted by the software in your host machine
after send
The print message in the server part. And you can see server only print message for the first request:
Got a connection from ('192.168.126.1', 60567)
('Server received', "u'GET<MACD.txt>'")
MACD, short for moving average convergence/divergence, is a trading indicator used in technical analysis of stock prices, created by Gerald Appel in the late 1970s.[1] It is supposed to reveal changes in the strength, direction, momentum, and duration of a trend in a stock's price.
I am confused by what I saw since I search the same problem in Stackoverflow and none of them match my case. I also read the document for python socket but still got nothing
I'm currently working on a project for a class. It consists in code a simple chat client (protocol given by the teacher) to do (at first) some simple tasks.
My problem is that after I send a mensage on the globlal channel or in other channel that doesn't require the use of a command, and try to send any command, the server replies with an error, saying something like: "msgbeforemsgbeforeCOMMAND" is not a valid command. I just cannot figure it out why this is happening...
(another thing, note that my dictionary is not printing the right why, I dont know why to)
ex:
chat running
import socket, select, string, sys
import threading
import time
def prompt():
sys.stdout.write('<You>: ')
sys.stdout.flush()
tLock = threading.Lock()
shutdown = False
def receber(sock):
while not shutdown:
try:
tLock.acquire()
while True:
data = sock.recv(1024)
if not data:
print ('Disconnected from server\n')
sys.exit()
else:
print ('<server>: %s' % (data.decode()))
sys.stdout.write(data)
except:
pass
finally:
tLock.release()
#Main Function
if __name__ == "__main__":
host = 'mini.alunos.di.uevora.pt'
port = 143
#IP do servidor
try:
busca_ip = socket.gethostbyname( host )
print ('Chat server IP: %s Port: %d \nNow connecting...\n' %(busca_ip, port))
except socket.gaierror:
#Não conseguiu o IP do servidor
print ('Hostname could not be resolved. Exiting.')
sys.exit()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(3)
# connectar ao host
try :
s.connect((busca_ip, port))
s.setblocking(0)
except :
print ('Unable to connect to the server.')
sys.exit()
print ('Connected to chat server. You may start chatting\n')
COM_ARG = {'_Comando_': '_Argumentos_',
'NICK': '<nickname> [\<password>]',
'MSG': '<recipient> \<message>',
'ENTER': '<room>',
'LEAVE': '<room> [\<message>]',
'RLIST':'',
'ULIST':''}
for chave, valor, in COM_ARG.items():
print (("%s %s") % (chave,valor))
print ('\n')
comandos = COM_ARG.keys()
#criar thread para o socket
t = threading.Thread(target = receber, args=(s,))
t.start()
while True:
msg = input('<You>: ')
msg = msg.strip()
msg12 = msg.upper()
msg12 = msg12.split()
try:
if msg12[0] in comandos:
msg = msg + '\n'
except:
pass
s.send(msg.encode())
time.sleep(0.25)
btw, sys.stdout.write(data) is doing something there?
Hope you could help me out.
(another thing, note that my dictionary is not printing the right why, I dont know why to)
Dictionary doesn't respect order.
My problem is that after I send a mensage on the globlal channel or in other channel that doesn't require the use of a command, and try to send any command, the server replies with an error, saying something like: "msgbeforemsgbeforeCOMMAND" is not a valid command. I just cannot figure it out why this is happening...
It's not just a problem with the code, the server recives the msgs, and keeps them until a '\n' appears, just then interprets the command. It's a "problem" with the protocol, but the code must be changed.
btw, sys.stdout.write(data) is doing something there?
Supposedly does the samething that print (data.decode()) does, but doesn't work in my case. I'm not sure.
I am creating a simple chat in python 3 using socket
here are the code
CLIENT
#!/bin/python
import socket
import threading
import time
tLock = threading.Lock()
poweroff = False
def receving(name, sock):
while not poweroff:
try:
tLock.acquire()
while True:
data, addr = sock.recvfrom(1024)
print (str(data))
except:
pass
finally:
tLock.release()
host = '127.0.0.1'
port = 0
server = ('127.0.0.1', 5000)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((host, port))
s.setblocking(0)
rT = threading.Thread(target=receving, args=("RecvThread", s))
rT.start()
alias = input("Username: ")
time.sleep(0.2)
message = input(alias + ">>> ")
while message != 'q':
if message != "":
s.sendto(str(alias + ": " + message).encode('utf-8'), server)
tLock.acquire()
message = input(alias + ">>> ")
tLock.release()
time.sleep(0.2)
poweroff = True
rT.join()
s.close()
SERVER
#!/bin/python
import socket
import time
hostname = '127.0.0.1'
port = 5000
clients = []
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((hostname, port))
s.setblocking(0)
iQuit = False
print ("Server Started.")
while not iQuit:
try:
data, addr = s.recvfrom(1024)
if addr not in clients:
clients.append(addr)
print (time.ctime(time.time()) + str(addr) + ": :" + str(data))
for client in clients:
s.sendto(data, client)
except:
pass
s.close()
How do i print a string to the server when a user connect?
I have tried to add this string after you have insert the name
s.sendto(str(alias + " Connected").encode('utf-8'), server)
but the output is orrible for me
Another Question:
Why i get something like this when seding a message?
Username: User_A
User_A>>> Hello
User_A>>> How Are you?
b'User:A: Hello'
User_A>>>
b'User_A: How Are you?'
b'User_B: Hi'
Concerning your second question: You are printing binary strings, see here for more information.
Use str(data.decode('utf-8')) instead of str(data) when printing the message on the server or the client.
Concerning the first question: This should work if you send the "Connected" string just after asking for the user name.
The string is decoded the same way as a common message if you include the decode('utf-8') and looks normal to me.
i have to press enter to see if user_B send something to me.
You enforced this behavior by locking out the receiving thread during the input of a message. You have to make up your mind whether you want this or want incoming data to be printed while typing.
You might want to cf. Simultaneous input and output for network based messaging program.
I'm learning TCP socket programming and I want to create a fake ftp client -- not using any ftp library. I have searched and read many articles, but did not find a clue how to login with username and password after the connection is built..
As you can see in the following code, the socket is created and the connection to the remote host can be successfully built, but then how can I send the username and password to the server?
(I run it on my terminal and it works)
I am very new to python, could you please give me some names of library or direction that I should go? Thank you very much.
import socket # For sockets
import sys
import getpass # Prompt the user for a password without echoing
# Starting: input host:
my_host = raw_input('myftp ')
# Build connection
try:
#create an AF_INET, STREAM socket (TCP)
my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg:
print 'Failed to create socket. Error code: ' + str(msg[0]) + ' , Error message : ' + msg[1]
sys.exit();
print "Socket Created!"
# get IP address...
try:
remote_ip = socket.gethostbyname(my_host)
except socket.gaierror:
#could not resolve
print 'Hostname could not be resolved. Exiting..'
sys.exit()
port = socket.getservbyname('http','tcp') # return port number
print "port: ", port
my_socket.connect((remote_ip,port))
print 'Socket connected to ' + my_host + '. The IP is ' + remote_ip
# Input user info
my_username = raw_input('Username: ')
my_password = getpass.getpass()
def CheckForLogin(target):
try:
server = (target,21)
user = "USER anonymous\r\n"
pwd = "PASS anonymous\r\n"
sock = socket.socket()
sock.connect(server)
sock.recv(4096)
sock.sendall(user.encode())
sock.recv(4096)
sock.sendall(pwd.encode())
answer = sock.recv(4096).decode('utf-8')
if "230" in answer:
open('found_ftp','a').write(target+":21 anonymous:anonymous\n")
sock.recv(4096)
print(sock.recv(120000))
elif "530" in answer:
sock.close()
sock.close()
except:
print("Login failed!\n")
pass