i am new to the python codes.. i just started out hello world.. i ended up getting an "invalid syntax" error... but i was able to run a TCP server program from the very same version of python which is python 2.3.4
Iam perplexed !!
">>> " belongs in the interactive window but not in the file being executed.
It's strange that the code you posted works. It has several indentation errors.
21 6799
Can you share your code here?
bvdet 2,851
Expert Mod 2GB
It could be something as simple as missing quotes. - >>> print hello world
-
Traceback ( File "<interactive input>", line 1
-
print hello world
-
^
-
SyntaxError: invalid syntax
-
>>> print "hello world"
-
hello world
-
>>>
D:\Python23>python hii.py
File "hii.py", line 1
Python 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bit (Intel)] on win
32
^
SyntaxError: invalid syntax
I got this when i tried to run the program from cmd line.
try to put quatos after print inside of hii.py:
print "hello world"
i still get the same error :-(
bvdet 2,851
Expert Mod 2GB
Please post the code you are using.
>>> h="hello world"
>>> print h
hello world
bvdet 2,851
Expert Mod 2GB
Remove the ">" characters and leading spaces. Your file should contain only:
not only this program , but in other basic programs also i get the same syntax error.. but no error comes in a this one.. i wonder how.. -
#Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
-
#Type "copyright", "credits" or "license()" for more information.
-
#import socket module
-
from socket import *
-
serverSocket = socket(AF_INET, SOCK_STREAM)
-
#Prepare a sever socket
-
host=gethostbyname(gethostname())
-
print host
-
port=1234
-
serverSocket.bind((host,port))
-
while True:
-
print 'Ready to serve...'
-
serverSocket.listen(1)
-
connectionSocket,addr=serverSocket.accept()
-
try:
-
message = connectionSocket.recv(4096)
-
filename = message.split()[1]
-
f = open(filename[1:])
-
outputdata = f.read()
-
#Send one HTTP header line into socket
-
#Fill in start
-
connectionSocket.send("200 OK")
-
#Fill in end
-
#Send the content of the requested file to the client
-
for i in range(0, len(outputdata)):
-
connectionSocket.send(outputdata[i])
-
connectionSocket.close()
-
except IOError:
-
#Send response message for file not found
-
#Fill in start
-
connectionSocket.send("404 file not found")
-
#Fill in end
-
#Close client socket
-
#Fill in start
-
connectionSocket.close()
-
#Fill in end
-
serverSocket.close()
when i type in the python GUI i am unable to remove the >>>
may b am troublin u a lot.. :-D
bvdet 2,851
Expert Mod 2GB
">>> " belongs in the interactive window but not in the file being executed.
It's strange that the code you posted works. It has several indentation errors.
actually the hello world program worked successfull when i did it without >>> .Thanks a lot.. but now another problem..
This program i wrote for server. -
from socket import *
-
s = socket(AF_INET,SOCK_STREAM)
-
h=gethostbyname(gethostname())
-
print'host is ',h
-
s.bind((h,9000))
-
s.listen(1)
-
while 1:
-
try:
-
client, address = s.accept()
-
print'connection from',address
-
data = client.recv(1024)
-
if not data:break
-
client.send(data)
-
client.close()
-
-
except IOError:
-
print'IOError is there !'
-
client.close()
i get this following error :
D:\Python23>python serv.py
File "serv.py", line 10
print"connection from",address
^
SyntaxError: invalid syntax
bvdet 2,851
Expert Mod 2GB
Your code is not indented properly. The interpreter expects an indentation after a try statement.
oh sorry.. i am new to the forum.. ll use code tag :)
i tried using indentation.. but the error comes in the print line following the client accept code. i.e 2nd line from try. :(
bvdet 2,851
Expert Mod 2GB
No problem. Code formatting does not display properly unless code tags are used. You can easily add them by selecting the text you want to wrap in tags and picking the "#" button at the top of the reply widget.
i rectified the error myself :) thanku sooo much.. :)
the error was all about indentation and proper port mentioning :-)
bvdet 2,851
Expert Mod 2GB
I suspect it is still an indentation problem. Your indentation should look like this: - from socket import *
-
s = socket(AF_INET,SOCK_STREAM)
-
h=gethostbyname(gethostname())
-
print 'host is ',h
-
s.bind((h,9000))
-
s.listen(1)
-
while 1:
-
try:
-
client, address = s.accept()
-
print 'connection from', address
-
data = client.recv(1024)
-
if not data:
-
break
-
client.send(data)
-
client.close()
-
-
except IOError:
-
print'IOError is there !'
-
client.close()
Note that the number of spaces representing an indentation is a matter of preference but should be consistent. I prefer 4 spaces. Also note it is bad practice to mix spaces and tabs.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Petr Prikryl |
last post by:
Hi,
Summary: In my opinion, the C-like prefix
increment and decrement operators (++i and --i)
should be marked as "syntax error".
Current situation: try... (Python 2.4 (#60, ...))
>>> i =...
|
by: frr |
last post by:
Hi,
After upgrading to 2.4 (from 2.3), I'm getting a weird syntax error:
>>> import themes
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "themes.py", line...
|
by: Berthold Höllmann |
last post by:
I have a default coding header
# -*- coding: iso-8859-15 -*-
in my python files. I now have Problems with this settings. I
swithched to Python 2.4.1 under Windows. When I import files with the...
|
by: py |
last post by:
Hi,
I am running python 2.4.2 on win xp pro. I have the WMI module from
Tim Golden (http://tgolden.sc.sabren.com/python/wmi.html).
I have some code which does this...
MyScript.py...
|
by: daniel |
last post by:
I'm not quite new to this language, but such error has never happened
so frequently before. I have no idea what to do about it, because
there's actually no syntax error at all !!
I'm using...
|
by: Michael Press |
last post by:
I have not written python codes nor run any. I saw this
code posted and decided to try it. It fails. I read the
tutorial and the entry for the built in function sum,
but still do not see the...
|
by: Dustin MacDonald |
last post by:
Hi everyone.
This is my first time posting to this newsgroup, and although I
maintain my netiquette I might've missed something specific to the
newsgroup, so hopefully you can avoid flaming me...
|
by: Karthik Gurusamy |
last post by:
I see python doesn't have ++ or -- operators unlike say, C.
I read some reasonings talking about immutable scalars and using ++/--
doesn't make much sense in python (not sure if ++i is that...
|
by: Alexis Boutillier |
last post by:
Hi,
I have a strange behaviour of python with pdb and import statement.
Here is the example code :
file my1.py:
import my2
file my2.py:
a=5
|
by: Timothy Grant |
last post by:
On Fri, Aug 29, 2008 at 12:48 AM, Alexis Boutillier
<alexis.boutillier@arteris.comwrote:
http://docs.python.org/ref/import.html
--
Stand Fast,
tjg.
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |