Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
The community is reviewing whether to reopen this question as of last year.
Improve this question
I need this for a project that I am making but I am not sure how to do it.
I'm look in for syntax like:
SENDER.py
string = "Hello"
send(hello)
READER.py
string = read()
print(string)
EDIT
Made a solution.
https://github.com/Ccode-lang/simpmsg
If both programmers are on different computers, you can try using sockets
server.py
import socket
s = socket.socket()
port = 12345
s.bind(('', port))
s.listen(5)
c, addr = s.accept()
print "Socket Up and running with a connection from",addr
while True:
rcvdData = c.recv(1024).decode()
print "S:",rcvdData
sendData = raw_input("N: ")
c.send(sendData.encode())
if(sendData == "Bye" or sendData == "bye"):
break
c.close()
client.py
import socket
s = socket.socket()
s.connect(('127.0.0.1',12345))
while True:
str = raw_input("S: ")
s.send(str.encode());
if(str == "Bye" or str == "bye"):
break
print "N:",s.recv(1024).decode()
s.close()
If you want to store it first so the other programmer can read it next time
use files
sender.py
file1 = open("myfile.txt","w")
file1.write("hello")
file1.close()
reader.py
file1 = open("myfile.txt","r")
data = file1.read()
file1.close()
print(data)
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 days ago.
Improve this question
I want to make it so it takes IP addresses from target list and scans port 22 and if it is open print(port 22 open)
also it takes proxies from a list and proxy's connection
Heres code:
import socket
import sys
import requests
ips = open('targets.txt')
x = ips.readline()
with open('proxyf.txt', 'r') as f:
proxy = f.read().splitlines()
proxies = {
'http': (proxy,)
}
print('Port 22 Bounty Hunter')
def attack():
try:
for port in range(22, 23):
requests.get(url='https://google.com', proxies=proxies)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((x, port))
if result == 0:
print("Port 22 is Open")
sock.close()
except socket.gaierror:
print('Hostname could not be resolved. Exiting')
sys.exit()
except socket.error:
print("Couldn't connect to server")
sys.exit()
for targets in range(100):
attack()
what is the error?
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am trying to establish a connection to server.py but client.py outputs this error
Traceback (most recent call last):
File "C:\Users\Nathan\Desktop\Coding\Langs\Python\Projects\Chatting Program\Client.py", line 15, in <module>
clientsocket.connect((host, port)) # Connects to the server
TypeError: an integer is required (got type str)
Here is my code...
## CLIENT.PY
from socket import *
import socket
host = input("Host: ")
port = input("Port: ")
#int(port)
username = input("Username: ")
username = "<" + username + ">"
print(f"Connecting under nick \"{username}\"")
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Creates socket
clientsocket.connect((host, port)) # Connects to the server
while True:
Csend = input("<MSG> ") # Input message
Csend = f"{username} {Csend}" # Add username to message
clientsocket.send(Csend) # Send message to ONLY the server
If there is an issue with my server.py then here is the code to that
## SERVER.PY
from socket import *
import socket
import select
host_name = socket.gethostname()
HOST = socket.gethostbyname(host_name)
PORT = 12345
print(f"Server Info\nHOST: {HOST}\nPORT: {PORT}")
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind((HOST, PORT))
serversocket.listen(5)
clientsocket, address = serversocket.accept()
print(address)
with clientsocket:
while True:
Srecv = clientsocket.recv(1024)
print(f"{username} - {address}: {Srecv}")
# Add server time to message before sending
clientsocket.sendall(Srecv)
I have tried converting host and port into str, int, and float, but it only successfully converts into str. Any help would be greatly appreciated. Thanks in advance!
The compile error is pretty fair: input() returns a string for the port number, whereas your function needs an integer. You can fix that by casting the port to an integer - your comment is close:
port = int(port).
If you look at the python documentation, input() always returns a string. The second value in the tuple passed to clientsocket.connect() must be a integer, however, you are passing a your string value. You must cast your port first, with the following code:
port = int(port).
#OR
port = int(input("Port: "))
Always check the docs!
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm testing responses for BrokenPipeError between a server application and a client application. I'm working on responses from the server to handle the BrokenPipeError appropriately. What would be the easiest way to break the connection (from the client side) in order to reproduce this error?
I am using standard python sockets as the connection.
TLDR: just exit the client while connected.
The simplest way I can see to break the connection from the client is to just close the client while you have a connection.
I quickly coded this example of a simple server-client in Python to demonstrate:
server.py:
import socket
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serverSocket.bind(("192.168.1.111", 6969))
serverSocket.listen(1)
conn, addr = serverSocket.accept()
while True:
print(conn.recv(1024).decode("utf-8"))
conn.send("Response".encode("utf-8"))
client.py:
import socket
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientSocket.connect(("192.168.1.111", 6969))
while True:
data_out = input("> ").encode("utf-8")
if len(data_out) > 0:
clientSocket.send(data_out)
print(clientSocket.recv(1024).decode("utf-8"))
This will let the client send some text and receive "Response" from the server, which it prints. The server will print whatever it receives and send back "Response". Simple.
All I had to do was exit the client.py while there was an active connetion and the server gave me this:
Traceback (most recent call last):
File "server.py", line 14, in <module>
conn.send("Response".encode("utf-8"))
BrokenPipeError: [Errno 32] Broken pipe
There's your broken pipe.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
#!/bin/python
import socket
HOST = "localhost"
PORT = 30002
list = []
passwd = "UoMYTrfrBFHyQXmg6gzctqAwOmw1IohZ"
for i in range(1000, 9999):
list.append(i)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
iter = 0
data = s.recv(1024)
# Brute forcing loop
while 1:
s.send(passwd + " " + list[iter]
data = s.recv(1024)
if "Fail!" not in data:
print s.recv(1024)
s.close()
else:
print "Not: " + list[iter]
iter += 1
s.close()
I get an invalid syntax on the s.recv call, but I believe that the socket isn't initiating a valid handshake. I can connect to the daemon through netcat.
You miss the parenthesis after the s.send() function
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
My Server TCP Code:
import socket
import sys
HOST = ''
PORT = 8031
s = socket.socket()
class BoServer:
def __init__(self):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error,msg:
print "Unable to create socket"
sys.exit()
print "Socket created."
def bind(self):
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
s.bind((HOST,PORT))
except socket.error,msg:
print "Bind failed. Closing..."
sys.exit()
print "Socket bound."
def run(self):
s.listen(10)
print "Socket Listening"
conn, addr = s.accept()
print "Connected to %s:%s"%(addr[0],addr[1])
while True:
income = conn.recv(4096)
if income != "":
print income
def main():
serv = BoServer()
serv.bind()
serv.run()
if __name__ == "__main__":
main()
My Client TCP Code:
import socket
import sys
def main():
host = ""
port = 8031
message = "Hello World!"
host = raw_input("Enter IP: ")
#Create Socket
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg:
print "Failed to create socket. Error code: %s Error Message: %s"%(str(msg[0]),msg[1])
sys.exit()
print "Socket created"
#Connect to server
s.connect((host,port))
while message != "/e":
#Send Data
message = raw_input("Send >> ")
try:
s.sendall(message)
except socket.error, msg:
print "ERROR %s"%(msg[1])
print "Failed to send."
s.close()
main()
I want to people write my public ip xxx.xxx.xxx.xx on the client and to connect at the server.
When I run the Server, others can't connect to my Server. I can only access the server with localhost or my local IP.
Everyone that tries to connect receive this ERROR:
Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time.
What am I doing wrong ?